File tree Expand file tree Collapse file tree 2 files changed +56
-5
lines changed Expand file tree Collapse file tree 2 files changed +56
-5
lines changed Original file line number Diff line number Diff line change @@ -349,11 +349,7 @@ function pjax(options) {
349
349
}
350
350
351
351
// Cancel the current request if we're already pjaxing
352
- var xhr = pjax . xhr
353
- if ( xhr && xhr . readyState < 4 ) {
354
- xhr . onreadystatechange = $ . noop
355
- xhr . abort ( )
356
- }
352
+ abortXHR ( pjax . xhr )
357
353
358
354
pjax . options = options
359
355
var xhr = pjax . xhr = $ . ajax ( options )
@@ -420,6 +416,12 @@ if ('state' in window.history) {
420
416
// You probably shouldn't use pjax on pages with other pushState
421
417
// stuff yet.
422
418
function onPjaxPopstate ( event ) {
419
+
420
+ // Hitting back or forward should override any pending PJAX request.
421
+ if ( ! initialPop ) {
422
+ abortXHR ( pjax . xhr )
423
+ }
424
+
423
425
var previousState = pjax . state
424
426
var state = event . state
425
427
@@ -536,6 +538,15 @@ function fallbackPjax(options) {
536
538
form . submit ( )
537
539
}
538
540
541
+ // Internal: Abort an XmlHttpRequest if it hasn't been completed,
542
+ // also removing its event handlers.
543
+ function abortXHR ( xhr ) {
544
+ if ( xhr && xhr . readyState < 4 ) {
545
+ xhr . onreadystatechange = $ . noop
546
+ xhr . abort ( )
547
+ }
548
+ }
549
+
539
550
// Internal: Generate unique id for state object.
540
551
//
541
552
// Use a timestamp instead of a counter since ids should still be
Original file line number Diff line number Diff line change @@ -845,6 +845,46 @@ if ($.support.pjax) {
845
845
} , 0 )
846
846
}
847
847
848
+ asyncTest ( "clicking back while loading cancels XHR" , function ( ) {
849
+ var frame = this . frame
850
+
851
+ frame . $ ( '#main' ) . on ( 'pjax:timeout' , function ( event ) {
852
+ event . preventDefault ( )
853
+ } )
854
+
855
+ frame . $ ( "#main" ) . one ( 'pjax:send' , function ( ) {
856
+
857
+ // Check that our request is aborted (need to check
858
+ // how robust this is across browsers)
859
+ frame . $ ( "#main" ) . one ( 'pjax:complete' , function ( e , xhr , textStatus ) {
860
+ equal ( xhr . status , 0 )
861
+ equal ( textStatus , 'abort' )
862
+ } )
863
+
864
+ setTimeout ( function ( ) {
865
+ frame . history . back ( )
866
+ } , 250 )
867
+
868
+ // Make sure the URL and content remain the same after the
869
+ // XHR would have arrived (delay on timeout.html is 1s)
870
+ setTimeout ( function ( ) {
871
+ var afterBackLocation = frame . location . pathname
872
+ var afterBackTitle = frame . document . title
873
+
874
+ setTimeout ( function ( ) {
875
+ equal ( frame . location . pathname , afterBackLocation )
876
+ equal ( frame . document . title , afterBackTitle )
877
+ start ( )
878
+ } , 1000 )
879
+ } , 500 )
880
+ } )
881
+
882
+ frame . $ . pjax ( {
883
+ url : "timeout.html" ,
884
+ container : "#main"
885
+ } )
886
+ } )
887
+
848
888
asyncTest ( "popstate going back to page" , function ( ) {
849
889
var frame = this . frame
850
890
You can’t perform that action at this time.
0 commit comments