2 rscript = /<script(.|\s)*?\/script>/g,
3 rselectTextarea = /select|textarea/i,
4 rinput = /text|hidden|password|search/i,
7 rts = /(\?|&)_=.*?(&|$)/,
8 rurl = /^(\w+:)?\/\/([^\/?#]+)/,
12 // Keep a copy of the old load
13 _load: jQuery.fn.load,
15 load: function( url, params, callback ) {
16 if ( typeof url !== "string" ) {
17 return this._load( url );
20 var off = url.indexOf(" ");
22 var selector = url.slice(off, url.length);
23 url = url.slice(0, off);
26 // Default to a GET request
29 // If the second parameter was provided
32 if ( jQuery.isFunction( params ) ) {
33 // We assume that it's the callback
37 // Otherwise, build a param string
38 } else if ( typeof params === "object" ) {
39 params = jQuery.param( params );
44 // Request the remote document
51 complete: function(res, status){
52 // If successful, inject the HTML into all the matched elements
53 if ( status === "success" || status === "notmodified" ) {
54 // See if a selector was specified
56 // Create a dummy div to hold the results
58 // inject the contents of the document in, removing the scripts
59 // to avoid any 'Permission Denied' errors in IE
60 .append(res.responseText.replace(rscript, ""))
62 // Locate the specified elements
65 // If not, just inject the full result
70 this.each( callback, [res.responseText, status, res] );
78 serialize: function() {
79 return jQuery.param(this.serializeArray());
81 serializeArray: function() {
82 return this.map(function(){
83 return this.elements ? jQuery.makeArray(this.elements) : this;
86 return this.name && !this.disabled &&
87 (this.checked || rselectTextarea.test(this.nodeName) ||
88 rinput.test(this.type));
90 .map(function(i, elem){
91 var val = jQuery(this).val();
96 jQuery.map( val, function(val, i){
97 return {name: elem.name, value: val};
99 {name: elem.name, value: val};
104 // Attach a bunch of functions for handling common AJAX events
105 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
106 jQuery.fn[o] = function(f){
107 return this.bind(o, f);
113 get: function( url, data, callback, type ) {
114 // shift arguments if data argument was omited
115 if ( jQuery.isFunction( data ) ) {
116 type = type || callback;
130 getScript: function( url, callback ) {
131 return jQuery.get(url, null, callback, "script");
134 getJSON: function( url, data, callback ) {
135 return jQuery.get(url, data, callback, "json");
138 post: function( url, data, callback, type ) {
139 // shift arguments if data argument was omited
140 if ( jQuery.isFunction( data ) ) {
141 type = type || callback;
155 ajaxSetup: function( settings ) {
156 jQuery.extend( jQuery.ajaxSettings, settings );
163 contentType: "application/x-www-form-urlencoded",
172 // Create the request object; Microsoft failed to properly
173 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
174 // This function can be overriden by calling jQuery.ajaxSetup
176 return window.ActiveXObject ?
177 new ActiveXObject("Microsoft.XMLHTTP") :
178 new XMLHttpRequest();
181 xml: "application/xml, text/xml",
183 script: "text/javascript, application/javascript",
184 json: "application/json, text/javascript",
190 // Last-Modified header cache for next request
194 ajax: function( s ) {
195 // Extend the settings, but re-extend 's' so that it can be
196 // checked again later (in the test suite, specifically)
197 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
199 var jsonp, status, data,
200 callbackContext = s.context || window,
201 type = s.type.toUpperCase();
203 // convert data if not already a string
204 if ( s.data && s.processData && typeof s.data !== "string" ) {
205 s.data = jQuery.param(s.data);
208 // Handle JSONP Parameter Callbacks
209 if ( s.dataType === "jsonp" ) {
210 if ( type === "GET" ) {
211 if ( !jsre.test( s.url ) ) {
212 s.url += (rquery.test( s.url ) ? "&" : "?") + (s.jsonp || "callback") + "=?";
214 } else if ( !s.data || !jsre.test(s.data) ) {
215 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
220 // Build temporary JSONP function
221 if ( s.dataType === "json" && (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
222 jsonp = "jsonp" + jsc++;
224 // Replace the =? sequence both in the query string and the data
226 s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
229 s.url = s.url.replace(jsre, "=" + jsonp + "$1");
231 // We need to make sure
232 // that a JSONP style response is executed properly
233 s.dataType = "script";
235 // Handle JSONP-style loading
236 window[ jsonp ] = function(tmp){
241 window[ jsonp ] = undefined;
242 try{ delete window[ jsonp ]; } catch(e){}
244 head.removeChild( script );
249 if ( s.dataType === "script" && s.cache === null ) {
253 if ( s.cache === false && type === "GET" ) {
256 // try replacing _= if it is there
257 var ret = s.url.replace(rts, "$1_=" + ts + "$2");
259 // if nothing was replaced, add timestamp to the end
260 s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
263 // If data is available, append data to url for get requests
264 if ( s.data && type === "GET" ) {
265 s.url += (rquery.test(s.url) ? "&" : "?") + s.data;
268 // Watch for a new set of requests
269 if ( s.global && ! jQuery.active++ ) {
270 jQuery.event.trigger( "ajaxStart" );
273 // Matches an absolute URL, and saves the domain
274 var parts = rurl.exec( s.url );
276 // If we're requesting a remote document
277 // and trying to load JSON or Script with a GET
278 if ( s.dataType === "script" && type === "GET" && parts
279 && ( parts[1] && parts[1] !== location.protocol || parts[2] !== location.host )) {
281 var head = document.getElementsByTagName("head")[0] || document.documentElement;
282 var script = document.createElement("script");
284 if ( s.scriptCharset ) {
285 script.charset = s.scriptCharset;
288 // Handle Script loading
292 // Attach handlers for all browsers
293 script.onload = script.onreadystatechange = function(){
294 if ( !done && (!this.readyState ||
295 this.readyState === "loaded" || this.readyState === "complete") ) {
300 // Handle memory leak in IE
301 script.onload = script.onreadystatechange = null;
302 if ( head && script.parentNode ) {
303 head.removeChild( script );
309 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
310 // This arises when a base node is used (#2709 and #4378).
311 head.insertBefore( script, head.firstChild );
313 // We handle everything using the script element injection
317 var requestDone = false;
319 // Create the request object
323 // Passing null username, generates a login popup on Opera (#2865)
325 xhr.open(type, s.url, s.async, s.username, s.password);
327 xhr.open(type, s.url, s.async);
330 // Need an extra try/catch for cross domain requests in Firefox 3
332 // Set the correct header, if data is being sent
334 xhr.setRequestHeader("Content-Type", s.contentType);
337 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
338 if ( s.ifModified ) {
339 if ( jQuery.lastModified[s.url] ) {
340 xhr.setRequestHeader("If-Modified-Since", jQuery.lastModified[s.url]);
343 if ( jQuery.etag[s.url] ) {
344 xhr.setRequestHeader("If-None-Match", jQuery.etag[s.url]);
348 // Set header so the called script knows that it's an XMLHttpRequest
349 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
351 // Set the Accepts header for the server, depending on the dataType
352 xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
353 s.accepts[ s.dataType ] + ", */*" :
354 s.accepts._default );
357 // Allow custom headers/mimetypes and early abort
358 if ( s.beforeSend && s.beforeSend.call(callbackContext, xhr, s) === false ) {
359 // Handle the global AJAX counter
360 if ( s.global && ! --jQuery.active ) {
361 jQuery.event.trigger( "ajaxStop" );
364 // close opended socket
370 trigger("ajaxSend", [xhr, s]);
373 // Wait for a response to come back
374 var onreadystatechange = function(isTimeout){
375 // The request was aborted, clear the interval and decrement jQuery.active
376 if ( !xhr || xhr.readyState === 0 ) {
378 // clear poll interval
379 clearInterval( ival );
382 // Handle the global AJAX counter
383 if ( s.global && ! --jQuery.active ) {
384 jQuery.event.trigger( "ajaxStop" );
388 // The transfer is complete and the data is available, or the request timed out
389 } else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
392 // clear poll interval
398 status = isTimeout === "timeout" ?
400 !jQuery.httpSuccess( xhr ) ?
402 s.ifModified && jQuery.httpNotModified( xhr, s.url ) ?
406 if ( status === "success" ) {
407 // Watch for, and catch, XML document parse errors
409 // process the data (runs the xml through httpData regardless of callback)
410 data = jQuery.httpData( xhr, s.dataType, s );
412 status = "parsererror";
416 // Make sure that the request was successful or notmodified
417 if ( status === "success" || status === "notmodified" ) {
418 // JSONP handles its own success callback
423 jQuery.handleError(s, xhr, status);
426 // Fire the complete handlers
441 // don't attach the handler to the request, just poll it instead
442 var ival = setInterval(onreadystatechange, 13);
445 if ( s.timeout > 0 ) {
446 setTimeout(function(){
447 // Check to see if the request is still happening
448 if ( xhr && !requestDone ) {
449 onreadystatechange( "timeout" );
457 xhr.send( type === "POST" || type === "PUT" ? s.data : null );
459 jQuery.handleError(s, xhr, null, e);
462 // firefox 1.5 doesn't fire statechange for sync requests
464 onreadystatechange();
468 // If a local callback was specified, fire it and pass it the data
470 s.success.call( callbackContext, data, status );
473 // Fire the global callback
475 trigger( "ajaxSuccess", [xhr, s] );
482 s.complete.call( callbackContext, xhr, status);
485 // The request was completed
487 trigger( "ajaxComplete", [xhr, s] );
490 // Handle the global AJAX counter
491 if ( s.global && ! --jQuery.active ) {
492 jQuery.event.trigger( "ajaxStop" );
496 function trigger(type, args){
497 (s.context ? jQuery(s.context) : jQuery.event).trigger(type, args);
500 // return XMLHttpRequest to allow aborting the request etc.
504 handleError: function( s, xhr, status, e ) {
505 // If a local callback was specified, fire it
507 s.error.call( s.context || window, xhr, status, e );
510 // Fire the global callback
512 (s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
516 // Counter for holding the number of active queries
519 // Determines if an XMLHttpRequest was successful or not
520 httpSuccess: function( xhr ) {
522 // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
523 return !xhr.status && location.protocol === "file:" ||
524 // Opera returns 0 when status is 304
525 ( xhr.status >= 200 && xhr.status < 300 ) ||
526 xhr.status === 304 || xhr.status === 1223 || xhr.status === 0;
532 // Determines if an XMLHttpRequest returns NotModified
533 httpNotModified: function( xhr, url ) {
534 var lastModified = xhr.getResponseHeader("Last-Modified"),
535 etag = xhr.getResponseHeader("Etag");
537 if ( lastModified ) {
538 jQuery.lastModified[url] = lastModified;
542 jQuery.etag[url] = etag;
545 // Opera returns 0 when status is 304
546 return xhr.status === 304 || xhr.status === 0;
549 httpData: function( xhr, type, s ) {
550 var ct = xhr.getResponseHeader("content-type"),
551 xml = type === "xml" || !type && ct && ct.indexOf("xml") >= 0,
552 data = xml ? xhr.responseXML : xhr.responseText;
554 if ( xml && data.documentElement.nodeName === "parsererror" ) {
558 // Allow a pre-filtering function to sanitize the response
559 // s is checked to keep backwards compatibility
560 if ( s && s.dataFilter ) {
561 data = s.dataFilter( data, type );
564 // The filter can actually parse the response
565 if ( typeof data === "string" ) {
567 // If the type is "script", eval it in global context
568 if ( type === "script" ) {
569 jQuery.globalEval( data );
572 // Get the JavaScript object, if JSON is used.
573 if ( type === "json" ) {
574 if ( typeof JSON === "object" && JSON.parse ) {
575 data = JSON.parse( data );
577 data = (new Function("return " + data))();
585 // Serialize an array of form elements or a set of
586 // key/values into a query string
587 param: function( a ) {
590 function add( key, value ){
591 s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
594 // If an array was passed in, assume that it is an array
596 if ( jQuery.isArray(a) || a.jquery ) {
597 // Serialize the form elements
598 jQuery.each( a, function() {
599 add( this.name, this.value );
602 // Recursively encode parameters from object,
603 // building a prefix path as we go down
604 function buildParams(obj, prefix)
606 if ( jQuery.isArray(obj) ) {
607 for ( var i = 0, length = obj.length; i < length; i++ ) {
608 buildParams( obj[i], prefix );
610 } else if( typeof(obj) == "object" ) {
611 for ( var j in obj ) {
612 var postfix = ((j.indexOf("[]") > 0) ? "[]" : ""); // move any brackets to the end
613 buildParams(obj[j], (prefix ? (prefix+"["+j.replace("[]", "")+"]"+postfix) : j) );
616 add( prefix, jQuery.isFunction(obj) ? obj() : obj );
622 // Return the resulting serialization
623 return s.join("&").replace(r20, "+");