Moved from the old JSMin to using YUIMin for compressing the jQuery source. Additiona...
[jquery.git] / src / ajax.js
index eb81e4c..c255dfe 100644 (file)
@@ -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,8 +66,7 @@ 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 &&
@@ -78,7 +76,7 @@ jQuery.fn.extend({
                .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,6 +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 ) ) {
@@ -151,6 +150,12 @@ jQuery.extend({
                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",
@@ -173,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
@@ -236,12 +241,13 @@ jQuery.extend({
                        jQuery.event.trigger( "ajaxStart" );
 
                // Matches an absolute URL, and saves the domain
-               var remote = /^(?:\w+:)?\/\/([^\/?#]+)/;
+               var parts = /^(\w+:)?\/\/([^\/?#]+)/.exec( s.url );
 
                // If we're requesting a remote document
                // and trying to load JSON or Script with a GET
-               if ( s.dataType == "script" && type == "GET"
-                               && remote.test(s.url) && remote.exec(s.url)[1] != location.host ){
+               if ( s.dataType == "script" && type == "GET" && parts
+                       && ( parts[1] && parts[1] != location.protocol || parts[2] != location.host )){
+
                        var head = document.getElementsByTagName("head")[0];
                        var script = document.createElement("script");
                        script.src = s.url;
@@ -272,9 +278,8 @@ jQuery.extend({
 
                var requestDone = false;
 
-               // Create the request object; Microsoft failed to properly
-               // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
-               var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
+               // Create the request object
+               var xhr = s.xhr();
 
                // Open the socket
                // Passing null username, generates a login popup on Opera (#2865)
@@ -303,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;
@@ -317,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
@@ -336,7 +352,7 @@ jQuery.extend({
                                        // Watch for, and catch, XML document parse errors
                                        try {
                                                // process the data (runs the xml through httpData regardless of callback)
-                                               data = jQuery.httpData( xhr, s.dataType, s.dataFilter );
+                                               data = jQuery.httpData( xhr, s.dataType, s );
                                        } catch(e) {
                                                status = "parsererror";
                                        }
@@ -377,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);
                }
@@ -442,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;
        },
@@ -454,13 +470,12 @@ 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;
        },
 
-       httpData: function( xhr, type, filter ) {
+       httpData: function( xhr, type, s ) {
                var ct = xhr.getResponseHeader("content-type"),
                        xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
                        data = xml ? xhr.responseXML : xhr.responseText;
@@ -469,31 +484,40 @@ jQuery.extend({
                        throw "parsererror";
                        
                // Allow a pre-filtering function to sanitize the response
-               if( filter )
-                       data = filter( data, type );
+               // 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;
        },
 
        // Serialize an array of form elements or a set of
        // key/values into a query string
        param: function( a ) {
-               var s = [];
+               var s = [ ];
+
+               function add( key, value ){
+                       s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
+               };
 
                // 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(){
-                               s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
+                               add( this.name, this.value );
                        });
 
                // Otherwise, assume that it's an object of key/value pairs
@@ -501,12 +525,12 @@ 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(){
-                                               s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
+                                               add( j, this );
                                        });
                                else
-                                       s.push( encodeURIComponent(j) + "=" + encodeURIComponent( jQuery.isFunction(a[j]) ? a[j]() : a[j] ) );
+                                       add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );
 
                // Return the resulting serialization
                return s.join("&").replace(/%20/g, "+");