Fix memory leaks in IE caused by the custom abort function of $.ajax. Fixes bug ...
[jquery.git] / src / ajax.js
index d06bbab..6ea61eb 100644 (file)
@@ -4,7 +4,7 @@ var jsc = jQuery.now(),
        rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
        rselectTextarea = /^(?:select|textarea)/i,
        rinput = /^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,
-       rnoContent = /^(?:GET|HEAD|DELETE)$/,
+       rnoContent = /^(?:GET|HEAD)$/,
        rbracket = /\[\]$/,
        jsre = /\=\?(&|$)/,
        rquery = /\?/,
@@ -195,6 +195,17 @@ jQuery.extend({
        },
 
        ajax: function( origSettings ) {
+               // IE8 leaks a lot when we've set abort, and IE6-8 a little
+               // when we have set onreadystatechange. Bug #6242
+               // XXX IE7 still leaks when abort is called, no matter what
+               // we do
+               function cleanup() {
+                       // IE6 will throw an error setting xhr.abort
+                       try {
+                               xhr.abort = xhr.onreadystatechange = jQuery.noop;
+                       } catch(e) {}
+               }
+
                var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings),
                        jsonp, status, data, type = s.type.toUpperCase(), noContent = rnoContent.test(type);
 
@@ -239,10 +250,6 @@ jQuery.extend({
                        var customJsonp = window[ jsonp ];
 
                        window[ jsonp ] = function( tmp ) {
-                               data = tmp;
-                               jQuery.handleSuccess( s, xhr, status, data );
-                               jQuery.handleComplete( s, xhr, status, data );
-                               
                                if ( jQuery.isFunction( customJsonp ) ) {
                                        customJsonp( tmp );
 
@@ -254,6 +261,10 @@ jQuery.extend({
                                                delete window[ jsonp ];
                                        } catch( jsonpError ) {}
                                }
+
+                               data = tmp;
+                               jQuery.handleSuccess( s, xhr, status, data );
+                               jQuery.handleComplete( s, xhr, status, data );
                                
                                if ( head ) {
                                        head.removeChild( script );
@@ -265,7 +276,7 @@ jQuery.extend({
                        s.cache = false;
                }
 
-               if ( s.cache === false && type === "GET" ) {
+               if ( s.cache === false && noContent ) {
                        var ts = jQuery.now();
 
                        // try replacing _= if it is there
@@ -275,8 +286,8 @@ jQuery.extend({
                        s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
                }
 
-               // If data is available, append data to url for get requests
-               if ( s.data && type === "GET" ) {
+               // If data is available, append data to url for GET/HEAD requests
+               if ( s.data && noContent ) {
                        s.url += (rquery.test(s.url) ? "&" : "?") + s.data;
                }
 
@@ -287,7 +298,7 @@ jQuery.extend({
 
                // Matches an absolute URL, and saves the domain
                var parts = rurl.exec( s.url ),
-                       remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
+                       remote = parts && (parts[1] && parts[1].toLowerCase() !== location.protocol || parts[2].toLowerCase() !== location.host);
 
                // If we're requesting a remote document
                // and trying to load JSON or Script with a GET
@@ -403,13 +414,12 @@ jQuery.extend({
 
                                requestDone = true;
                                if ( xhr ) {
-                                       xhr.onreadystatechange = jQuery.noop;
+                                       cleanup();
                                }
 
                        // The transfer is complete and the data is available, or the request timed out
                        } else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
                                requestDone = true;
-                               xhr.onreadystatechange = jQuery.noop;
 
                                status = isTimeout === "timeout" ?
                                        "timeout" :
@@ -451,10 +461,7 @@ jQuery.extend({
                                        xhr.abort();
                                }
 
-                               // Stop memory leaks
-                               if ( s.async ) {
-                                       xhr = null;
-                               }
+                               cleanup();
                        }
                };
 
@@ -463,10 +470,11 @@ jQuery.extend({
                try {
                        var oldAbort = xhr.abort;
                        xhr.abort = function() {
-                               // xhr.abort in IE7 is not a native JS function
-                               // and does not have a call property
-                               if ( xhr && oldAbort.call ) {
-                                       oldAbort.call( xhr );
+                               if ( xhr ) {
+                                       // oldAbort has no call property in IE7 so
+                                       // just do it this way, which works in all
+                                       // browsers
+                                       Function.prototype.call.call( oldAbort, xhr );
                                }
 
                                onreadystatechange( "abort" );
@@ -506,11 +514,12 @@ jQuery.extend({
        // Serialize an array of form elements or a set of
        // key/values into a query string
        param: function( a, traditional ) {
-               var s = [], add = function( key, value ) {
-                       // If value is a function, invoke it and return its value
-                       value = jQuery.isFunction(value) ? value() : value;
-                       s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
-               };
+               var s = [],
+                       add = function( key, value ) {
+                               // If value is a function, invoke it and return its value
+                               value = jQuery.isFunction(value) ? value() : value;
+                               s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
+                       };
                
                // Set traditional to true for jQuery <= 1.3.2 behavior.
                if ( traditional === undefined ) {