(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 {
} 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 ?
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,
// Do not keep as active anymore
if ( handle ) {
xhr.onreadystatechange = jQuery.noop;
- delete xhrs[ handle ];
+ delete xhrCallbacks[ handle ];
}
// If it's an abort
xhr.abort();
}
} else {
- // Get info
status = xhr.status;
responseHeaders = xhr.getAllResponseHeaders();
responses = {};
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;
}
},