Skip to content
Permalink
Browse files

Makes sure xhrs are actually aborted on unload in IE. Simplifies acti…

…ve xhrs caching in the process.
  • Loading branch information
jaubourg committed Feb 7, 2011
1 parent d6fbbe1 commit 0c21c83e9691a08151f23a2c594568b07009f063
Showing with 26 additions and 34 deletions.
  1. +26 −34 src/ajax/xhr.js
@@ -1,5 +1,22 @@
(function( jQuery ) {

var // #5280: next active xhr id and list of active xhrs' callbacks
xhrId = jQuery.now(),
xhrCallbacks,

// XHR used to determine supports properties
testXHR;

// #5280: Internet Explorer will keep connections alive if we don't abort on unload
function xhrOnUnloadAbort() {
jQuery( window ).unload(function() {

This comment has been minimized.

Copy link
@jdalton

jdalton Apr 16, 2011

Member

If this is executed for other browsers besides IE it will disable backward-forward caching.

// Abort all pending requests
for ( var key in xhrCallbacks ) {
xhrCallbacks[ key ]( 0, 1 );
}
});
}

// Functions to create xhrs
function createStandardXHR() {
try {
@@ -13,18 +30,6 @@ function createActiveXHR() {
} catch( e ) {}
}

var // Next active xhr id
xhrId = jQuery.now(),

// active xhrs
xhrs = {},

// #5280: see below
xhrUnloadAbortInstalled,

// XHR used to determine supports properties
testXHR;

// Create the request object
// (This is still attached to ajaxSettings for backward compatibility)
jQuery.ajaxSettings.xhr = window.ActiveXObject ?
@@ -62,23 +67,6 @@ if ( jQuery.support.ajax ) {
return {
send: function( headers, complete ) {

// #5280: we need to abort on unload or IE will keep connections alive
if ( !xhrUnloadAbortInstalled ) {

xhrUnloadAbortInstalled = 1;

jQuery(window).bind( "unload", function() {

// Abort all pending requests
jQuery.each( xhrs, function( _, xhr ) {
if ( xhr.onreadystatechange ) {
xhr.onreadystatechange( 1 );
}
} );

} );
}

// Get a new xhr
var xhr = s.xhr(),
handle,
@@ -142,7 +130,7 @@ if ( jQuery.support.ajax ) {
// Do not keep as active anymore
if ( handle ) {
xhr.onreadystatechange = jQuery.noop;
delete xhrs[ handle ];
delete xhrCallbacks[ handle ];
}

// If it's an abort
@@ -152,7 +140,6 @@ if ( jQuery.support.ajax ) {
xhr.abort();
}
} else {
// Get info
status = xhr.status;
responseHeaders = xhr.getAllResponseHeaders();
responses = {};
@@ -223,10 +210,15 @@ if ( jQuery.support.ajax ) {
if ( !s.async || xhr.readyState === 4 ) {
callback();
} else {
// Add to list of active xhrs
// Create the active xhrs callbacks list if needed
// and attach the unload handler
if ( !xhrCallbacks ) {
xhrCallbacks = {};
xhrOnUnloadAbort();
}
// Add to list of active xhrs callbacks
handle = xhrId++;
xhrs[ handle ] = xhr;
xhr.onreadystatechange = callback;
xhr.onreadystatechange = xhrCallbacks[ handle ] = callback;
}
},

0 comments on commit 0c21c83

Please sign in to comment.
You can’t perform that action at this time.