jquery ajax: misc optimization for $.fn.load().
[jquery.git] / src / ajax.js
index 3e43ffb..bda79b5 100644 (file)
@@ -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 {
+                       } 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;
@@ -97,6 +96,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 +151,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",
@@ -236,12 +242,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 +279,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)
@@ -336,7 +342,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";
                                        }
@@ -460,7 +466,7 @@ jQuery.extend({
                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 +475,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 = 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 )
                        // 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
@@ -503,10 +518,10 @@ jQuery.extend({
                                // If the value is an array then the key names need to be repeated
                                if ( a[j] && a[j].constructor == Array )
                                        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, "+");