X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fajax.js;h=18d0f96c2f18cdb72a3674df38158bb2c5787081;hb=f8ef75eb9124ce924be5fb521c783efd5c996e33;hp=441a7b9bf67b21d7838a2ac9250726b9be83c329;hpb=b8ceedbe2ba07f87a0f0e3862dfe012e8aa863b3;p=jquery.git diff --git a/src/ajax.js b/src/ajax.js index 441a7b9..18d0f96 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(" "); @@ -12,8 +12,6 @@ jQuery.fn.extend({ url = url.slice(0, off); } - callback = callback || function(){}; - // Default to a GET request var type = "GET"; @@ -26,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"; } @@ -56,7 +54,8 @@ jQuery.fn.extend({ // If not, just inject the full result res.responseText ); - self.each( callback, [res.responseText, status, res] ); + if( callback ) + self.each( callback, [res.responseText, status, res] ); } }); return this; @@ -67,18 +66,17 @@ jQuery.fn.extend({ }, serializeArray: function() { return this.map(function(){ - return jQuery.nodeName(this, "form") ? - jQuery.makeArray(this.elements) : this; + return this.elements ? jQuery.makeArray(this.elements) : this; }) .filter(function(){ return this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || - /text|hidden|password/i.test(this.type)); + /text|hidden|password|search/i.test(this.type)); }) .map(function(i, elem){ var val = jQuery(this).val(); return val == null ? null : - val.constructor == Array ? + jQuery.isArray(val) ? jQuery.map( val, function(val, i){ return {name: elem.name, value: val}; }) : @@ -97,7 +95,7 @@ jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".sp var jsc = now(); jQuery.extend({ - + get: function( url, data, callback, type ) { // shift arguments if data argument was ommited if ( jQuery.isFunction( data ) ) { @@ -113,13 +111,6 @@ jQuery.extend({ dataType: type }); }, - - // Create the request object; Microsoft failed to properly - // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available - getAjaxTransport : function() { - return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); - }, - getScript: function( url, callback ) { return jQuery.get(url, null, callback, "script"); @@ -152,13 +143,21 @@ jQuery.extend({ url: location.href, global: true, type: "GET", - timeout: 0, contentType: "application/x-www-form-urlencoded", processData: true, async: true, + /* + timeout: 0, data: null, username: null, password: null, + */ + // Create the request object; Microsoft failed to properly + // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available + // This function can be overriden by calling jQuery.ajaxSetup + xhr:function(){ + return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); + }, accepts: { xml: "application/xml, text/xml", html: "text/html", @@ -181,7 +180,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 @@ -268,6 +267,9 @@ jQuery.extend({ done = true; success(); complete(); + + // Handle memory leak in IE + script.onload = script.onreadystatechange = null; head.removeChild( script ); } }; @@ -282,7 +284,7 @@ jQuery.extend({ var requestDone = false; // Create the request object - var xhr = jQuery.getAjaxTransport(); + var xhr = s.xhr(); // Open the socket // Passing null username, generates a login popup on Opera (#2865) @@ -311,10 +313,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; @@ -325,8 +328,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 @@ -370,6 +383,9 @@ jQuery.extend({ // Fire the complete handlers complete(); + if ( isTimeout ) + xhr.abort(); + // Stop memory leaks if ( s.async ) xhr = null; @@ -384,13 +400,8 @@ jQuery.extend({ if ( s.timeout > 0 ) setTimeout(function(){ // Check to see if the request is still happening - if ( xhr ) { - // Cancel the request - xhr.abort(); - - if( !requestDone ) - onreadystatechange( "timeout" ); - } + if ( xhr && !requestDone ) + onreadystatechange( "timeout" ); }, s.timeout); } @@ -450,8 +461,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; }, @@ -462,8 +472,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; }, @@ -475,19 +484,23 @@ jQuery.extend({ if ( xml && data.documentElement.tagName == "parsererror" ) throw "parsererror"; - + // Allow a pre-filtering function to sanitize the response // s != null is checked to keep backwards compatibility if( s && s.dataFilter ) data = s.dataFilter( data, type ); - // If the type is "script", eval it in global context - if ( type == "script" ) - jQuery.globalEval( data ); + // The filter can actually parse the response + if( typeof data === "string" ){ - // Get the JavaScript object, if JSON is used. - if ( type == "json" ) - data = eval("(" + data + ")"); + // If the type is "script", eval it in global context + if ( type == "script" ) + jQuery.globalEval( data ); + + // Get the JavaScript object, if JSON is used. + if ( type == "json" ) + data = window["eval"]("(" + data + ")"); + } return data; }, @@ -503,7 +516,7 @@ jQuery.extend({ // If an array was passed in, assume that it is an array // of form elements - if ( a.constructor == Array || a.jquery ) + if ( jQuery.isArray(a) || a.jquery ) // Serialize the form elements jQuery.each( a, function(){ add( this.name, this.value ); @@ -514,7 +527,7 @@ jQuery.extend({ // Serialize the key/values for ( var j in a ) // If the value is an array then the key names need to be repeated - if ( a[j] && a[j].constructor == Array ) + if ( jQuery.isArray(a[j]) ) jQuery.each( a[j], function(){ add( j, this ); });