Skip to content

Commit a0011cc

Browse files
committed
Move XHR abort code into a function.
1 parent d13d1a5 commit a0011cc

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

jquery.pjax.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,7 @@ function pjax(options) {
339339
}
340340

341341
// Cancel the current request if we're already pjaxing
342-
var xhr = pjax.xhr
343-
if ( xhr && xhr.readyState < 4) {
344-
xhr.onreadystatechange = $.noop
345-
xhr.abort()
346-
}
342+
abortXHR(pjax.xhr)
347343

348344
pjax.options = options
349345
var xhr = pjax.xhr = $.ajax(options)
@@ -409,12 +405,8 @@ if ('state' in window.history) {
409405
// stuff yet.
410406
function onPjaxPopstate(event) {
411407

412-
// Cancel the current request if we're already pjaxing
413-
var xhr = pjax.xhr
414-
if ( xhr && xhr.readyState < 4) {
415-
xhr.onreadystatechange = $.noop
416-
xhr.abort()
417-
}
408+
// hitting back or forward should override any pending PJAX request
409+
abortXHR(pjax.xhr);
418410

419411
var state = event.state
420412

@@ -518,6 +510,16 @@ function fallbackPjax(options) {
518510
form.submit()
519511
}
520512

513+
514+
// Internal: Abort an XmlHttpRequest if it hasn't been completed,
515+
// also removing its event handlers.
516+
function abortXHR(xhr) {
517+
if ( xhr && xhr.readyState < 4) {
518+
xhr.onreadystatechange = $.noop
519+
xhr.abort()
520+
}
521+
}
522+
521523
// Internal: Generate unique id for state object.
522524
//
523525
// Use a timestamp instead of a counter since ids should still be

0 commit comments

Comments
 (0)