diff options
author | Richard Gibson <richard.gibson@gmail.com> | 2012-11-23 10:29:08 -0500 |
---|---|---|
committer | Richard Gibson <richard.gibson@gmail.com> | 2012-11-25 13:07:46 -0500 |
commit | a938d7b1282fc0e5c52502c225ae8f0cef219f0a (patch) | |
tree | e65720e5c0ab9c55690cabf16f0a7423c1c2daa7 /src/ajax | |
parent | c27dc018e22260b6ad084dff4505172e6b107178 (diff) | |
download | jquery-a938d7b1282fc0e5c52502c225ae8f0cef219f0a.tar.gz jquery-a938d7b1282fc0e5c52502c225ae8f0cef219f0a.zip |
No ticket: compress ajax. Close gh-1041.
Diffstat (limited to 'src/ajax')
-rw-r--r-- | src/ajax/jsonp.js | 31 | ||||
-rw-r--r-- | src/ajax/script.js | 19 | ||||
-rw-r--r-- | src/ajax/xhr.js | 34 |
3 files changed, 38 insertions, 46 deletions
diff --git a/src/ajax/jsonp.js b/src/ajax/jsonp.js index c39913fac..b8803df44 100644 --- a/src/ajax/jsonp.js +++ b/src/ajax/jsonp.js @@ -1,13 +1,11 @@ var oldCallbacks = [], - rquestion = /\?/, - rjsonp = /(=)\?(?=&|$)|\?\?/, - nonce = jQuery.now(); + rjsonp = /(=)\?(?=&|$)|\?\?/; // Default jsonp settings jQuery.ajaxSetup({ jsonp: "callback", jsonpCallback: function() { - var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) ); + var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( ajax_nonce++ ) ); this[ callback ] = true; return callback; } @@ -17,30 +15,24 @@ jQuery.ajaxSetup({ jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) { var callbackName, overwritten, responseContainer, - data = s.data, - url = s.url, - hasCallback = s.jsonp !== false, - replaceInUrl = hasCallback && rjsonp.test( url ), - replaceInData = hasCallback && !replaceInUrl && typeof data === "string" && - !( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") && - rjsonp.test( data ); + jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ? + "url" : + typeof s.data === "string" && !( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") && rjsonp.test( s.data ) && "data" + ); // Handle iff the expected data type is "jsonp" or we have a parameter to set - if ( s.dataTypes[ 0 ] === "jsonp" || replaceInUrl || replaceInData ) { + if ( jsonProp || s.dataTypes[ 0 ] === "jsonp" ) { // Get callback name, remembering preexisting value associated with it callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback; - overwritten = window[ callbackName ]; // Insert callback into url or form data - if ( replaceInUrl ) { - s.url = url.replace( rjsonp, "$1" + callbackName ); - } else if ( replaceInData ) { - s.data = data.replace( rjsonp, "$1" + callbackName ); - } else if ( hasCallback ) { - s.url += ( rquestion.test( url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName; + if ( jsonProp ) { + s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName ); + } else if ( s.jsonp !== false ) { + s.url += ( ajax_rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName; } // Use data converter to retrieve json after script execution @@ -55,6 +47,7 @@ jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) { s.dataTypes[ 0 ] = "json"; // Install callback + overwritten = window[ callbackName ]; window[ callbackName ] = function() { responseContainer = arguments; }; diff --git a/src/ajax/script.js b/src/ajax/script.js index ada74ff67..ac14c44be 100644 --- a/src/ajax/script.js +++ b/src/ajax/script.js @@ -4,7 +4,7 @@ jQuery.ajaxSetup({ script: "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript" }, contents: { - script: /javascript|ecmascript/ + script: /(?:java|ecma)script/ }, converters: { "text script": function( text ) { @@ -32,13 +32,13 @@ jQuery.ajaxTransport( "script", function(s) { if ( s.crossDomain ) { var script, - head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement; + head = document.head || jQuery("head")[0] || document.documentElement; return { send: function( _, callback ) { - script = document.createElement( "script" ); + script = document.createElement("script"); script.async = true; @@ -57,12 +57,12 @@ jQuery.ajaxTransport( "script", function(s) { script.onload = script.onreadystatechange = null; // Remove the script - if ( head && script.parentNode ) { - head.removeChild( script ); + if ( script.parentNode ) { + script.parentNode.removeChild( script ); } // Dereference the script - script = undefined; + script = null; // Callback if not abort if ( !isAbort ) { @@ -70,14 +70,15 @@ jQuery.ajaxTransport( "script", function(s) { } } }; - // Use insertBefore instead of appendChild to circumvent an IE6 bug. - // This arises when a base node is used (#2709 and #4378). + + // Circumvent IE6 bugs with base elements (#2709 and #4378) by prepending + // Use native DOM manipulation to avoid our domManip AJAX trickery head.insertBefore( script, head.firstChild ); }, abort: function() { if ( script ) { - script.onload( 0, 1 ); + script.onload( undefined, true ); } } }; diff --git a/src/ajax/xhr.js b/src/ajax/xhr.js index d3422ec79..d00e1f125 100644 --- a/src/ajax/xhr.js +++ b/src/ajax/xhr.js @@ -1,12 +1,13 @@ -var xhrCallbacks, +var xhrCallbacks, xhrSupported, + xhrId = 0, // #5280: Internet Explorer will keep connections alive if we don't abort on unload - xhrOnUnloadAbort = window.ActiveXObject ? function() { + xhrOnUnloadAbort = window.ActiveXObject && function() { // Abort all pending requests - for ( var key in xhrCallbacks ) { - xhrCallbacks[ key ]( 0, 1 ); + var key; + for ( key in xhrCallbacks ) { + xhrCallbacks[ key ]( undefined, true ); } - } : false, - xhrId = 0; + }; // Functions to create xhrs function createStandardXHR() { @@ -17,7 +18,7 @@ function createStandardXHR() { function createActiveXHR() { try { - return new window.ActiveXObject( "Microsoft.XMLHTTP" ); + return new window.ActiveXObject("Microsoft.XMLHTTP"); } catch( e ) {} } @@ -37,15 +38,12 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ? createStandardXHR; // Determine support properties -(function( xhr ) { - jQuery.extend( jQuery.support, { - ajax: !!xhr, - cors: !!xhr && ( "withCredentials" in xhr ) - }); -})( jQuery.ajaxSettings.xhr() ); +xhrSupported = jQuery.ajaxSettings.xhr(); +jQuery.support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported ); +xhrSupported = jQuery.support.ajax = !!xhrSupported; // Create transport if the browser can provide an xhr -if ( jQuery.support.ajax ) { +if ( xhrSupported ) { jQuery.ajaxTransport(function( s ) { // Cross domain only allowed if supported through XMLHttpRequest @@ -86,7 +84,7 @@ if ( jQuery.support.ajax ) { // (it can always be set on a per-request basis or even using ajaxSetup) // For same-domain requests, won't change header if already provided. if ( !s.crossDomain && !headers["X-Requested-With"] ) { - headers[ "X-Requested-With" ] = "XMLHttpRequest"; + headers["X-Requested-With"] = "XMLHttpRequest"; } // Need an extra try/catch for cross domain requests in Firefox 3 @@ -136,10 +134,10 @@ if ( jQuery.support.ajax ) { xhr.abort(); } } else { - status = xhr.status; - responseHeaders = xhr.getAllResponseHeaders(); responses = {}; + status = xhr.status; xml = xhr.responseXML; + responseHeaders = xhr.getAllResponseHeaders(); // Construct response list if ( xml && xml.documentElement /* #4958 */ ) { @@ -211,7 +209,7 @@ if ( jQuery.support.ajax ) { abort: function() { if ( callback ) { - callback(0,1); + callback( undefined, true ); } } }; |