2 // Keep a copy of the old load
5 load: function( url, params, callback ) {
6 if ( typeof url !== "string" )
7 return this._load( url );
9 var off = url.indexOf(" ");
11 var selector = url.slice(off, url.length);
12 url = url.slice(0, off);
15 // Default to a GET request
18 // If the second parameter was provided
21 if ( jQuery.isFunction( params ) ) {
22 // We assume that it's the callback
26 // Otherwise, build a param string
27 } else if( typeof params === "object" ) {
28 params = jQuery.param( params );
34 // Request the remote document
40 complete: function(res, status){
41 // If successful, inject the HTML into all the matched elements
42 if ( status == "success" || status == "notmodified" )
43 // See if a selector was specified
45 // Create a dummy div to hold the results
47 // inject the contents of the document in, removing the scripts
48 // to avoid any 'Permission Denied' errors in IE
49 .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))
51 // Locate the specified elements
54 // If not, just inject the full result
58 self.each( callback, [res.responseText, status, res] );
64 serialize: function() {
65 return jQuery.param(this.serializeArray());
67 serializeArray: function() {
68 return this.map(function(){
69 return this.elements ? jQuery.makeArray(this.elements) : this;
72 return this.name && !this.disabled &&
73 (this.checked || /select|textarea/i.test(this.nodeName) ||
74 /text|hidden|password/i.test(this.type));
76 .map(function(i, elem){
77 var val = jQuery(this).val();
78 return val == null ? null :
80 jQuery.map( val, function(val, i){
81 return {name: elem.name, value: val};
83 {name: elem.name, value: val};
88 // Attach a bunch of functions for handling common AJAX events
89 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
90 jQuery.fn[o] = function(f){
91 return this.bind(o, f);
99 get: function( url, data, callback, type ) {
100 // shift arguments if data argument was ommited
101 if ( jQuery.isFunction( data ) ) {
115 getScript: function( url, callback ) {
116 return jQuery.get(url, null, callback, "script");
119 getJSON: function( url, data, callback ) {
120 return jQuery.get(url, data, callback, "json");
123 post: function( url, data, callback, type ) {
124 if ( jQuery.isFunction( data ) ) {
138 ajaxSetup: function( settings ) {
139 jQuery.extend( jQuery.ajaxSettings, settings );
147 contentType: "application/x-www-form-urlencoded",
153 // Create the request object; Microsoft failed to properly
154 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
155 // This function can be overriden by calling jQuery.ajaxSetup
157 return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
160 xml: "application/xml, text/xml",
162 script: "text/javascript, application/javascript",
163 json: "application/json, text/javascript",
169 // Last-Modified header cache for next request
172 ajax: function( s ) {
173 // Extend the settings, but re-extend 's' so that it can be
174 // checked again later (in the test suite, specifically)
175 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
177 var jsonp, jsre = /=\?(&|$)/g, status, data,
178 type = s.type.toUpperCase();
180 // convert data if not already a string
181 if ( s.data && s.processData && typeof s.data !== "string" )
182 s.data = jQuery.param(s.data);
184 // Handle JSONP Parameter Callbacks
185 if ( s.dataType == "jsonp" ) {
186 if ( type == "GET" ) {
187 if ( !s.url.match(jsre) )
188 s.url += (s.url.match(/\?/) ? "&" : "?") + (s.jsonp || "callback") + "=?";
189 } else if ( !s.data || !s.data.match(jsre) )
190 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
194 // Build temporary JSONP function
195 if ( s.dataType == "json" && (s.data && s.data.match(jsre) || s.url.match(jsre)) ) {
196 jsonp = "jsonp" + jsc++;
198 // Replace the =? sequence both in the query string and the data
200 s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
201 s.url = s.url.replace(jsre, "=" + jsonp + "$1");
203 // We need to make sure
204 // that a JSONP style response is executed properly
205 s.dataType = "script";
207 // Handle JSONP-style loading
208 window[ jsonp ] = function(tmp){
213 window[ jsonp ] = undefined;
214 try{ delete window[ jsonp ]; } catch(e){}
216 head.removeChild( script );
220 if ( s.dataType == "script" && s.cache == null )
223 if ( s.cache === false && type == "GET" ) {
225 // try replacing _= if it is there
226 var ret = s.url.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2");
227 // if nothing was replaced, add timestamp to the end
228 s.url = ret + ((ret == s.url) ? (s.url.match(/\?/) ? "&" : "?") + "_=" + ts : "");
231 // If data is available, append data to url for get requests
232 if ( s.data && type == "GET" ) {
233 s.url += (s.url.match(/\?/) ? "&" : "?") + s.data;
235 // IE likes to send both get and post data, prevent this
239 // Watch for a new set of requests
240 if ( s.global && ! jQuery.active++ )
241 jQuery.event.trigger( "ajaxStart" );
243 // Matches an absolute URL, and saves the domain
244 var parts = /^(\w+:)?\/\/([^\/?#]+)/.exec( s.url );
246 // If we're requesting a remote document
247 // and trying to load JSON or Script with a GET
248 if ( s.dataType == "script" && type == "GET" && parts
249 && ( parts[1] && parts[1] != location.protocol || parts[2] != location.host )){
251 var head = document.getElementsByTagName("head")[0];
252 var script = document.createElement("script");
255 script.charset = s.scriptCharset;
257 // Handle Script loading
261 // Attach handlers for all browsers
262 script.onload = script.onreadystatechange = function(){
263 if ( !done && (!this.readyState ||
264 this.readyState == "loaded" || this.readyState == "complete") ) {
268 head.removeChild( script );
273 head.appendChild(script);
275 // We handle everything using the script element injection
279 var requestDone = false;
281 // Create the request object
285 // Passing null username, generates a login popup on Opera (#2865)
287 xhr.open(type, s.url, s.async, s.username, s.password);
289 xhr.open(type, s.url, s.async);
291 // Need an extra try/catch for cross domain requests in Firefox 3
293 // Set the correct header, if data is being sent
295 xhr.setRequestHeader("Content-Type", s.contentType);
297 // Set the If-Modified-Since header, if ifModified mode.
299 xhr.setRequestHeader("If-Modified-Since",
300 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
302 // Set header so the called script knows that it's an XMLHttpRequest
303 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
305 // Set the Accepts header for the server, depending on the dataType
306 xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
307 s.accepts[ s.dataType ] + ", */*" :
308 s.accepts._default );
311 // Allow custom headers/mimetypes and early abort
312 if ( s.beforeSend && s.beforeSend(xhr, s) === false ) {
313 // Handle the global AJAX counter
314 if ( s.global && ! --jQuery.active )
315 jQuery.event.trigger( "ajaxStop" );
316 // close opended socket
322 jQuery.event.trigger("ajaxSend", [xhr, s]);
324 // Wait for a response to come back
325 var onreadystatechange = function(isTimeout){
326 // The request was aborted, clear the interval and decrement jQuery.active
327 if (xhr.readyState == 0) {
329 // clear poll interval
332 // Handle the global AJAX counter
333 if ( s.global && ! --jQuery.active )
334 jQuery.event.trigger( "ajaxStop" );
336 // The transfer is complete and the data is available, or the request timed out
337 } else if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) {
340 // clear poll interval
346 status = isTimeout == "timeout" ? "timeout" :
347 !jQuery.httpSuccess( xhr ) ? "error" :
348 s.ifModified && jQuery.httpNotModified( xhr, s.url ) ? "notmodified" :
351 if ( status == "success" ) {
352 // Watch for, and catch, XML document parse errors
354 // process the data (runs the xml through httpData regardless of callback)
355 data = jQuery.httpData( xhr, s.dataType, s );
357 status = "parsererror";
361 // Make sure that the request was successful or notmodified
362 if ( status == "success" ) {
363 // Cache Last-Modified header, if ifModified mode.
366 modRes = xhr.getResponseHeader("Last-Modified");
367 } catch(e) {} // swallow exception thrown by FF if header is not available
369 if ( s.ifModified && modRes )
370 jQuery.lastModified[s.url] = modRes;
372 // JSONP handles its own success callback
376 jQuery.handleError(s, xhr, status);
378 // Fire the complete handlers
388 // don't attach the handler to the request, just poll it instead
389 var ival = setInterval(onreadystatechange, 13);
393 setTimeout(function(){
394 // Check to see if the request is still happening
397 onreadystatechange( "timeout" );
399 // Cancel the request
410 jQuery.handleError(s, xhr, null, e);
413 // firefox 1.5 doesn't fire statechange for sync requests
415 onreadystatechange();
418 // If a local callback was specified, fire it and pass it the data
420 s.success( data, status );
422 // Fire the global callback
424 jQuery.event.trigger( "ajaxSuccess", [xhr, s] );
430 s.complete(xhr, status);
432 // The request was completed
434 jQuery.event.trigger( "ajaxComplete", [xhr, s] );
436 // Handle the global AJAX counter
437 if ( s.global && ! --jQuery.active )
438 jQuery.event.trigger( "ajaxStop" );
441 // return XMLHttpRequest to allow aborting the request etc.
445 handleError: function( s, xhr, status, e ) {
446 // If a local callback was specified, fire it
447 if ( s.error ) s.error( xhr, status, e );
449 // Fire the global callback
451 jQuery.event.trigger( "ajaxError", [xhr, s, e] );
454 // Counter for holding the number of active queries
457 // Determines if an XMLHttpRequest was successful or not
458 httpSuccess: function( xhr ) {
460 // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
461 return !xhr.status && location.protocol == "file:" ||
462 ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 ||
463 jQuery.browser.safari && xhr.status === undefined;
468 // Determines if an XMLHttpRequest returns NotModified
469 httpNotModified: function( xhr, url ) {
471 var xhrRes = xhr.getResponseHeader("Last-Modified");
473 // Firefox always returns 200. check Last-Modified date
474 return xhr.status == 304 || xhrRes == jQuery.lastModified[url] ||
475 jQuery.browser.safari && xhr.status === undefined;
480 httpData: function( xhr, type, s ) {
481 var ct = xhr.getResponseHeader("content-type"),
482 xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
483 data = xml ? xhr.responseXML : xhr.responseText;
485 if ( xml && data.documentElement.tagName == "parsererror" )
488 // Allow a pre-filtering function to sanitize the response
489 // s != null is checked to keep backwards compatibility
490 if( s && s.dataFilter )
491 data = s.dataFilter( data, type );
493 // The filter can actually parse the response
494 if( typeof data === "string" ){
496 // If the type is "script", eval it in global context
497 if ( type == "script" )
498 jQuery.globalEval( data );
500 // Get the JavaScript object, if JSON is used.
501 if ( type == "json" )
502 data = eval("(" + data + ")");
508 // Serialize an array of form elements or a set of
509 // key/values into a query string
510 param: function( a ) {
513 function add( key, value ){
514 s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
517 // If an array was passed in, assume that it is an array
519 if ( jQuery.isArray(a) || a.jquery )
520 // Serialize the form elements
521 jQuery.each( a, function(){
522 add( this.name, this.value );
525 // Otherwise, assume that it's an object of key/value pairs
527 // Serialize the key/values
529 // If the value is an array then the key names need to be repeated
530 if ( jQuery.isArray(a[j]) )
531 jQuery.each( a[j], function(){
535 add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );
537 // Return the resulting serialization
538 return s.join("&").replace(/%20/g, "+");