X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fajax.js;h=c255dfebaaeafb6247a13e29eb2d1d4453b57722;hb=434b87b8a233eb24ec773de801f3adb460fbd0f4;hp=7e024a319732d41f18cfa79b27d29ac10f67a2c7;hpb=325755d4b38e87b13f9c24a4c2991ec497552aad;p=jquery.git diff --git a/src/ajax.js b/src/ajax.js index 7e024a3..c255dfe 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -3,7 +3,7 @@ jQuery.fn.extend({ _load: jQuery.fn.load, load: function( url, params, callback ) { - if ( typeof url != 'string' ) + if ( typeof url !== "string" ) return this._load( url ); var off = url.indexOf(" "); @@ -24,7 +24,7 @@ jQuery.fn.extend({ params = null; // Otherwise, build a param string - } else if( typeof params == 'object' ) { + } else if( typeof params === "object" ) { params = jQuery.param( params ); type = "POST"; } @@ -178,7 +178,7 @@ jQuery.extend({ type = s.type.toUpperCase(); // convert data if not already a string - if ( s.data && s.processData && typeof s.data != "string" ) + if ( s.data && s.processData && typeof s.data !== "string" ) s.data = jQuery.param(s.data); // Handle JSONP Parameter Callbacks @@ -308,10 +308,11 @@ jQuery.extend({ s.accepts._default ); } catch(e){} - // Allow custom headers/mimetypes + // Allow custom headers/mimetypes and early abort if ( s.beforeSend && s.beforeSend(xhr, s) === false ) { - // cleanup active request counter - s.global && jQuery.active--; + // Handle the global AJAX counter + if ( s.global && ! --jQuery.active ) + jQuery.event.trigger( "ajaxStop" ); // close opended socket xhr.abort(); return false; @@ -322,8 +323,18 @@ jQuery.extend({ // Wait for a response to come back var onreadystatechange = function(isTimeout){ + // The request was aborted, clear the interval and decrement jQuery.active + if (xhr.readyState == 0) { + if (ival) { + // clear poll interval + clearInterval(ival); + ival = null; + // Handle the global AJAX counter + if ( s.global && ! --jQuery.active ) + jQuery.event.trigger( "ajaxStop" ); + } // The transfer is complete and the data is available, or the request timed out - if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { + } else if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { requestDone = true; // clear poll interval @@ -382,11 +393,12 @@ jQuery.extend({ setTimeout(function(){ // Check to see if the request is still happening if ( xhr ) { - // Cancel the request - xhr.abort(); - if( !requestDone ) onreadystatechange( "timeout" ); + + // Cancel the request + if ( xhr ) + xhr.abort(); } }, s.timeout); } @@ -447,8 +459,7 @@ jQuery.extend({ try { // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450 return !xhr.status && location.protocol == "file:" || - ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 || - jQuery.browser.safari && xhr.status == undefined; + ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223; } catch(e){} return false; }, @@ -459,8 +470,7 @@ jQuery.extend({ var xhrRes = xhr.getResponseHeader("Last-Modified"); // Firefox always returns 200. check Last-Modified date - return xhr.status == 304 || xhrRes == jQuery.lastModified[url] || - jQuery.browser.safari && xhr.status == undefined; + return xhr.status == 304 || xhrRes == jQuery.lastModified[url]; } catch(e){} return false; }, @@ -479,7 +489,7 @@ jQuery.extend({ data = s.dataFilter( data, type ); // The filter can actually parse the response - if( typeof data == 'string' ){ + if( typeof data === "string" ){ // If the type is "script", eval it in global context if ( type == "script" ) @@ -487,7 +497,7 @@ jQuery.extend({ // Get the JavaScript object, if JSON is used. if ( type == "json" ) - data = eval("(" + data + ")"); + data = window["eval"]("(" + data + ")"); } return data;