Makes sure xhrs are actually aborted on unload in IE. Simplifies active xhrs caching in the process.

This commit is contained in:
jaubourg 2011-02-07 06:11:52 +01:00
parent d6fbbe1080
commit 0c21c83e96

View File

@ -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() {
// 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;
}
},