diff options
author | Timmy Willison <timmywillisn@gmail.com> | 2016-01-05 17:36:14 -0500 |
---|---|---|
committer | Timmy Willison <timmywillisn@gmail.com> | 2016-01-06 14:05:17 -0500 |
commit | a79ccf4b185f2789e8df4043004a256cc279d969 (patch) | |
tree | f0a08cf7866c6d419b35d0581e886809162c1540 | |
parent | 373607aa78ce35de10ca12e5bc693a7e375336f9 (diff) | |
download | jquery-a79ccf4b185f2789e8df4043004a256cc279d969.tar.gz jquery-a79ccf4b185f2789e8df4043004a256cc279d969.zip |
Revert "Ajax: remove "onunload" event handler"
This reverts commit 4632e55870a00a70e737a862cdb0820278cec0fe.
-rw-r--r-- | src/ajax/xhr.js | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/src/ajax/xhr.js b/src/ajax/xhr.js index 6502d24c2..7e4140971 100644 --- a/src/ajax/xhr.js +++ b/src/ajax/xhr.js @@ -40,7 +40,20 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject !== undefined ? // For all other browsers, use the standard XMLHttpRequest object createStandardXHR; -var xhrSupported = jQuery.ajaxSettings.xhr(); +var xhrId = 0, + xhrCallbacks = {}, + xhrSupported = jQuery.ajaxSettings.xhr(); + +// Support: IE<10 +// Open requests must be manually aborted on unload (#5280) +// See https://support.microsoft.com/kb/2856746 for more info +if ( window.attachEvent ) { + window.attachEvent( "onunload", function() { + for ( var key in xhrCallbacks ) { + xhrCallbacks[ key ]( undefined, true ); + } + } ); +} // Determine support properties support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported ); @@ -59,7 +72,8 @@ if ( xhrSupported ) { return { send: function( headers, complete ) { var i, - xhr = options.xhr(); + xhr = options.xhr(), + id = ++xhrId; // Open the socket xhr.open( @@ -105,6 +119,11 @@ if ( xhrSupported ) { } } + // Do send the request + // This may raise an exception which is actually + // handled in jQuery.ajax (so no try/catch here) + xhr.send( ( options.hasContent && options.data ) || null ); + // Listener callback = function( _, isAbort ) { var status, statusText, responses; @@ -113,6 +132,7 @@ if ( xhrSupported ) { if ( callback && ( isAbort || xhr.readyState === 4 ) ) { // Clean up + delete xhrCallbacks[ id ]; callback = undefined; xhr.onreadystatechange = jQuery.noop; @@ -168,8 +188,6 @@ if ( xhrSupported ) { // handled in jQuery.ajax (so no try/catch here) if ( !options.async ) { - xhr.send( ( options.hasContent && options.data ) || null ); - // If we're in sync mode we fire the callback callback(); } else if ( xhr.readyState === 4 ) { @@ -180,13 +198,8 @@ if ( xhrSupported ) { } else { // Register the callback, but delay it in case `xhr.send` throws - xhr.onreadystatechange = function() { - if ( callback ) { - window.setTimeout( callback ); - } - }; - - xhr.send( ( options.hasContent && options.data ) || null ); + // Add to the list of active xhr callbacks + xhr.onreadystatechange = xhrCallbacks[ id ] = callback; } }, |