X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fajax%2Fxhr.js;h=3c3d12493f74f7d222fd7fe935a7dcb829e88bb6;hb=667a3b31e6a27cfe3f209765d583bff584ecc2f6;hp=7d714025933ad46c22847c1987e9ca9ffd3cd3c7;hpb=c43b078c6911027fd4124d542446ad0098662f6a;p=jquery.git diff --git a/src/ajax/xhr.js b/src/ajax/xhr.js index 7d71402..3c3d124 100644 --- a/src/ajax/xhr.js +++ b/src/ajax/xhr.js @@ -6,8 +6,11 @@ var // Next fake timer id // Callbacks hashtable xhrs = {}, - // #5280: see end of file - xhrUnloadAbortMarker = []; + // XHR pool + xhrPool = [], + + // #5280: see below + xhrUnloadAbortInstalled; jQuery.ajax.transport( function( s , determineDataType ) { @@ -21,7 +24,25 @@ jQuery.ajax.transport( function( s , determineDataType ) { send: function(headers, complete) { - var xhr = s.xhr(), + // #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 = xhrPool.pop() || s.xhr(), handle; // Open the socket @@ -35,7 +56,7 @@ jQuery.ajax.transport( function( s , determineDataType ) { // Requested-With header // Not set for crossDomain requests with no content // (see why at http://trac.dojotoolkit.org/ticket/9486) - // Won't change header if already provided in beforeSend + // Won't change header if already provided if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) { headers["x-requested-with"] = "XMLHttpRequest"; } @@ -53,45 +74,44 @@ jQuery.ajax.transport( function( s , determineDataType ) { try { xhr.send( ( s.hasContent && s.data ) || null ); } catch(e) { + // Store back into pool + xhrPool.push( xhr ); complete(0, "error", "" + e); return; } // Listener - callback = function ( abortStatusText ) { + callback = function( isAbort ) { // Was never called and is aborted or complete - if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) { + if ( callback && ( isAbort || xhr.readyState === 4 ) ) { + + // Only called once + callback = 0; - // Do not listen anymore + // Do not keep as active anymore + // and store back into pool if (handle) { xhr.onreadystatechange = jQuery.noop; delete xhrs[ handle ]; handle = undefined; + xhrPool.push( xhr ); } - callback = 0; - - // Get info - var status, statusText, response, responseHeaders; - - if ( abortStatusText ) { + // If it's an abort + if ( isAbort ) { + // Abort it manually if needed if ( xhr.readyState !== 4 ) { xhr.abort(); } - - // Stop here if unloadAbort - if ( abortStatusText === xhrUnloadAbortMarker ) { - return; - } - - status = 0; - statusText = abortStatusText; - } else { - status = xhr.status; + // Get info + var status = xhr.status, + statusText, + response, + responseHeaders = xhr.getAllResponseHeaders(); try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests @@ -103,8 +123,6 @@ jQuery.ajax.transport( function( s , determineDataType ) { } - responseHeaders = xhr.getAllResponseHeaders(); - // Filter status for non standard behaviours // (so many they seem to be the actual "standard") status = @@ -130,19 +148,17 @@ jQuery.ajax.transport( function( s , determineDataType ) { status ); - // Guess response if needed & update datatype accordingly - if ( status >= 200 && status < 300 ) { - response = - determineDataType( - s, - xhr.getResponseHeader("content-type"), - xhr.responseText, - xhr.responseXML ); - } - } + // Guess response & update dataType accordingly + response = + determineDataType( + s, + xhr.getResponseHeader("content-type"), + xhr.responseText, + xhr.responseXML ); - // Call complete - complete(status,statusText,response,responseHeaders); + // Call complete + complete(status,statusText,response,responseHeaders); + } } }; @@ -164,28 +180,13 @@ jQuery.ajax.transport( function( s , determineDataType ) { } }, - abort: function(statusText) { + abort: function() { if ( callback ) { - callback(statusText); + callback(1); } } }; } }); -// #5280: we need to abort on unload or IE will keep connections alive -jQuery(window).bind( "unload" , function() { - - // Abort all pending requests - jQuery.each(xhrs, function(_, xhr) { - if ( xhr.onreadystatechange ) { - xhr.onreadystatechange( xhrUnloadAbortMarker ); - } - }); - - // Resest polling structure to be safe - xhrs = {}; - -}); - })( jQuery );