3 var rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
4 rselectTextarea = /^(?:select|textarea)/i,
5 rinput = /^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,
10 // Keep a copy of the old load method
11 _load = jQuery.fn.load;
14 load: function( url, params, callback ) {
15 if ( typeof url !== "string" && _load ) {
16 return _load.apply( this, arguments );
18 // Don't do a request if no elements are being requested
19 } else if ( !this.length ) {
23 var off = url.indexOf(" ");
25 var selector = url.slice(off, url.length);
26 url = url.slice(0, off);
29 // Default to a GET request
32 // If the second parameter was provided
35 if ( jQuery.isFunction( params ) ) {
36 // We assume that it's the callback
40 // Otherwise, build a param string
41 } else if ( typeof params === "object" ) {
42 params = jQuery.param( params, jQuery.ajaxSettings.traditional );
49 // Request the remote document
55 complete: function( res, status ) {
56 // If successful, inject the HTML into all the matched elements
57 if ( status === "success" || status === "notmodified" ) {
58 // See if a selector was specified
60 // Create a dummy div to hold the results
62 // inject the contents of the document in, removing the scripts
63 // to avoid any 'Permission Denied' errors in IE
64 .append(res.responseText.replace(rscript, ""))
66 // Locate the specified elements
69 // If not, just inject the full result
74 self.each( callback, [res.responseText, status, res] );
82 serialize: function() {
83 return jQuery.param(this.serializeArray());
86 serializeArray: function() {
87 return this.map(function(){
88 return this.elements ? jQuery.makeArray(this.elements) : this;
91 return this.name && !this.disabled &&
92 (this.checked || rselectTextarea.test(this.nodeName) ||
93 rinput.test(this.type));
95 .map(function(i, elem){
96 var val = jQuery(this).val();
100 jQuery.isArray(val) ?
101 jQuery.map( val, function(val, i){
102 return {name: elem.name, value: val};
104 {name: elem.name, value: val};
109 // Attach a bunch of functions for handling common AJAX events
110 jQuery.each( "ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".split(" "), function(i,o){
111 jQuery.fn[o] = function(f){
112 return this.bind(o, f);
116 jQuery.each( [ "get", "post" ], function( i, method ) {
117 jQuery[ method ] = function( url, data, callback, type ) {
118 // shift arguments if data argument was omited
119 if ( jQuery.isFunction( data ) ) {
120 type = type || callback;
137 getScript: function( url, callback ) {
138 return jQuery.get(url, null, callback, "script");
141 getJSON: function( url, data, callback ) {
142 return jQuery.get(url, data, callback, "json");
145 ajaxSetup: function( settings ) {
146 jQuery.extend( jQuery.ajaxSettings, settings );
153 contentType: "application/x-www-form-urlencoded",
167 return new window.XMLHttpRequest();
171 xml: "application/xml, text/xml",
174 json: "application/json, text/javascript",
185 // 1) They are useful to introduce custom dataTypes (see transport/jsonp for an example)
186 // 2) These are called:
187 // * BEFORE asking for a transport
188 // * AFTER param serialization (s.data is a string if s.processData is true)
189 // 3) key is the dataType
190 // 4) the catchall symbol "*" can be used
191 // 5) execution will start with transport dataType and THEN continue down to "*" if needed
194 // Transports bindings
195 // 1) key is the dataType
196 // 2) the catchall symbol "*" can be used
197 // 3) selection will start with transport dataType and THEN go to "*" if needed
200 // List of data converters
201 // 1) key format is "source_type destination_type" (a single space in-between)
202 // 2) the catchall symbol "*" can be used for source_type
205 // Convert anything to text
206 "* text": window.String,
208 // Text to html (true = no transformation)
211 // Evaluate text as a json expression
212 "text json": jQuery.parseJSON,
215 "text xml": jQuery.parseXML
220 ajax: function( url , s ) {
222 if ( arguments.length === 1 ) {
224 url = s ? s.url : undefined;
227 return jQuery.xhr().open( s ? s.type : undefined , url ).send( undefined , s );
231 // Serialize an array of form elements or a set of
232 // key/values into a query string
233 param: function( a, traditional ) {
235 add = function( key, value ) {
236 // If value is a function, invoke it and return its value
237 value = jQuery.isFunction(value) ? value() : value;
238 s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
241 // Set traditional to true for jQuery <= 1.3.2 behavior.
242 if ( traditional === undefined ) {
243 traditional = jQuery.ajaxSettings.traditional;
246 // If an array was passed in, assume that it is an array of form elements.
247 if ( jQuery.isArray(a) || a.jquery ) {
248 // Serialize the form elements
249 jQuery.each( a, function() {
250 add( this.name, this.value );
254 // If traditional, encode the "old" way (the way 1.3.2 or older
255 // did it), otherwise encode params recursively.
256 for ( var prefix in a ) {
257 buildParams( prefix, a[prefix], traditional, add );
261 // Return the resulting serialization
262 return s.join("&").replace(r20, "+");
266 function buildParams( prefix, obj, traditional, add ) {
267 if ( jQuery.isArray(obj) && obj.length ) {
268 // Serialize array item.
269 jQuery.each( obj, function( i, v ) {
270 if ( traditional || rbracket.test( prefix ) ) {
271 // Treat each array item as a scalar.
275 // If array item is non-scalar (array or object), encode its
276 // numeric index to resolve deserialization ambiguity issues.
277 // Note that rack (as of 1.0.0) can't currently deserialize
278 // nested arrays properly, and attempting to do so may cause
279 // a server error. Possible fixes are to modify rack's
280 // deserialization algorithm or to provide an option or flag
281 // to force array serialization to be shallow.
282 buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v, traditional, add );
286 } else if ( !traditional && obj != null && typeof obj === "object" ) {
287 // If we see an array here, it is empty and should be treated as an empty
289 if ( jQuery.isArray( obj ) || jQuery.isEmptyObject( obj ) ) {
292 // Serialize object item.
294 jQuery.each( obj, function( k, v ) {
295 buildParams( prefix + "[" + k + "]", v, traditional, add );
300 // Serialize scalar item.
305 // This is still on the jQuery object... for now
306 // Want to move this to jQuery.ajax some day
309 // Counter for holding the number of active queries
312 // Last-Modified header cache for next request
319 * Create the request object; Microsoft failed to properly
320 * implement the XMLHttpRequest in IE7 (can't request local files),
321 * so we use the ActiveXObject when it is available
322 * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
323 * we need a fallback.
325 if ( window.ActiveXObject ) {
326 jQuery.ajaxSettings.xhr = function() {
327 if ( window.location.protocol !== "file:" ) {
329 return new window.XMLHttpRequest();
330 } catch( xhrError ) {}
334 return new window.ActiveXObject("Microsoft.XMLHTTP");
335 } catch( activeError ) {}
339 var testXHR = jQuery.ajaxSettings.xhr();
341 // Does this browser support XHR requests?
342 jQuery.support.ajax = !!testXHR;
344 // Does this browser support crossDomain XHR requests
345 jQuery.support.cors = testXHR && "withCredentials" in testXHR;