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 );
46 // Request the remote document
52 complete: function(res, status){
53 // If successful, inject the HTML into all the matched elements
54 if ( status === "success" || status === "notmodified" ) {
55 // See if a selector was specified
57 // Create a dummy div to hold the results
59 // inject the contents of the document in, removing the scripts
60 // to avoid any 'Permission Denied' errors in IE
61 .append(res.responseText.replace(rscript, ""))
63 // Locate the specified elements
66 // If not, just inject the full result
71 self.each( callback, [res.responseText, status, res] );
79 serialize: function() {
80 return jQuery.param(this.serializeArray());
82 serializeArray: function() {
83 return this.map(function(){
84 return this.elements ? jQuery.makeArray(this.elements) : this;
87 return this.name && !this.disabled &&
88 (this.checked || rselectTextarea.test(this.nodeName) ||
89 rinput.test(this.type));
91 .map(function(i, elem){
92 var val = jQuery(this).val();
97 jQuery.map( val, function(val, i){
98 return {name: elem.name, value: val};
100 {name: elem.name, value: val};
105 // Attach a bunch of functions for handling common AJAX events
106 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
107 jQuery.fn[o] = function(f){
108 return this.bind(o, f);
114 get: function( url, data, callback, type ) {
115 // shift arguments if data argument was ommited
116 if ( jQuery.isFunction( data ) ) {
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 if ( jQuery.isFunction( data ) ) {
153 ajaxSetup: function( settings ) {
154 jQuery.extend( jQuery.ajaxSettings, settings );
161 contentType: "application/x-www-form-urlencoded",
170 // Create the request object; Microsoft failed to properly
171 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
172 // This function can be overriden by calling jQuery.ajaxSetup
174 return window.ActiveXObject ?
175 new ActiveXObject("Microsoft.XMLHTTP") :
176 new XMLHttpRequest();
179 xml: "application/xml, text/xml",
181 script: "text/javascript, application/javascript",
182 json: "application/json, text/javascript",
188 // Last-Modified header cache for next request
192 ajax: function( s ) {
193 // Extend the settings, but re-extend 's' so that it can be
194 // checked again later (in the test suite, specifically)
195 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
197 var jsonp, status, data,
198 type = s.type.toUpperCase();
200 // convert data if not already a string
201 if ( s.data && s.processData && typeof s.data !== "string" ) {
202 s.data = jQuery.param(s.data);
205 // Handle JSONP Parameter Callbacks
206 if ( s.dataType === "jsonp" ) {
207 if ( type === "GET" ) {
208 if ( !jsre.test( s.url ) ) {
209 s.url += (rquery.test( s.url ) ? "&" : "?") + (s.jsonp || "callback") + "=?";
211 } else if ( !s.data || !jsre.test(s.data) ) {
212 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
217 // Build temporary JSONP function
218 if ( s.dataType === "json" && (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
219 jsonp = "jsonp" + jsc++;
221 // Replace the =? sequence both in the query string and the data
223 s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
226 s.url = s.url.replace(jsre, "=" + jsonp + "$1");
228 // We need to make sure
229 // that a JSONP style response is executed properly
230 s.dataType = "script";
232 // Handle JSONP-style loading
233 window[ jsonp ] = function(tmp){
238 window[ jsonp ] = undefined;
239 try{ delete window[ jsonp ]; } catch(e){}
241 head.removeChild( script );
246 if ( s.dataType === "script" && s.cache === null ) {
250 if ( s.cache === false && type === "GET" ) {
253 // try replacing _= if it is there
254 var ret = s.url.replace(rts, "$1_=" + ts + "$2");
256 // if nothing was replaced, add timestamp to the end
257 s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
260 // If data is available, append data to url for get requests
261 if ( s.data && type === "GET" ) {
262 s.url += (rquery.test(s.url) ? "&" : "?") + s.data;
265 // Watch for a new set of requests
266 if ( s.global && ! jQuery.active++ ) {
267 jQuery.event.trigger( "ajaxStart" );
270 // Matches an absolute URL, and saves the domain
271 var parts = rurl.exec( s.url );
273 // If we're requesting a remote document
274 // and trying to load JSON or Script with a GET
275 if ( s.dataType === "script" && type === "GET" && parts
276 && ( parts[1] && parts[1] !== location.protocol || parts[2] !== location.host )) {
278 var head = document.getElementsByTagName("head")[0];
279 var script = document.createElement("script");
281 if ( s.scriptCharset ) {
282 script.charset = s.scriptCharset;
285 // Handle Script loading
289 // Attach handlers for all browsers
290 script.onload = script.onreadystatechange = function(){
291 if ( !done && (!this.readyState ||
292 this.readyState === "loaded" || this.readyState === "complete") ) {
297 // Handle memory leak in IE
298 script.onload = script.onreadystatechange = null;
299 if ( head && script.parentNode ) {
300 head.removeChild( script );
306 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
307 // This arises when a base node is used (#2709 and #4378).
308 head.insertBefore( script, head.firstChild );
310 // We handle everything using the script element injection
314 var requestDone = false;
316 // Create the request object
320 // Passing null username, generates a login popup on Opera (#2865)
322 xhr.open(type, s.url, s.async, s.username, s.password);
324 xhr.open(type, s.url, s.async);
327 // Need an extra try/catch for cross domain requests in Firefox 3
329 // Set the correct header, if data is being sent
331 xhr.setRequestHeader("Content-Type", s.contentType);
334 // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
335 if ( s.ifModified ) {
336 if ( jQuery.lastModified[s.url] ) {
337 xhr.setRequestHeader("If-Modified-Since", jQuery.lastModified[s.url]);
340 if ( jQuery.etag[s.url] ) {
341 xhr.setRequestHeader("If-None-Match", jQuery.etag[s.url]);
345 // Set header so the called script knows that it's an XMLHttpRequest
346 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
348 // Set the Accepts header for the server, depending on the dataType
349 xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
350 s.accepts[ s.dataType ] + ", */*" :
351 s.accepts._default );
354 // Allow custom headers/mimetypes and early abort
355 if ( s.beforeSend && s.beforeSend(xhr, s) === false ) {
356 // Handle the global AJAX counter
357 if ( s.global && ! --jQuery.active ) {
358 jQuery.event.trigger( "ajaxStop" );
361 // close opended socket
367 jQuery.event.trigger("ajaxSend", [xhr, s]);
370 // Wait for a response to come back
371 var onreadystatechange = function(isTimeout){
372 // The request was aborted, clear the interval and decrement jQuery.active
373 if ( xhr.readyState === 0 ) {
375 // clear poll interval
376 clearInterval( ival );
379 // Handle the global AJAX counter
380 if ( s.global && ! --jQuery.active ) {
381 jQuery.event.trigger( "ajaxStop" );
385 // The transfer is complete and the data is available, or the request timed out
386 } else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
389 // clear poll interval
395 status = isTimeout === "timeout" ?
397 !jQuery.httpSuccess( xhr ) ?
399 s.ifModified && jQuery.httpNotModified( xhr, s.url ) ?
403 if ( status === "success" ) {
404 // Watch for, and catch, XML document parse errors
406 // process the data (runs the xml through httpData regardless of callback)
407 data = jQuery.httpData( xhr, s.dataType, s );
409 status = "parsererror";
413 // Make sure that the request was successful or notmodified
414 if ( status === "success" || status === "notmodified" ) {
415 // JSONP handles its own success callback
420 jQuery.handleError(s, xhr, status);
423 // Fire the complete handlers
438 // don't attach the handler to the request, just poll it instead
439 var ival = setInterval(onreadystatechange, 13);
442 if ( s.timeout > 0 ) {
443 setTimeout(function(){
444 // Check to see if the request is still happening
445 if ( xhr && !requestDone ) {
446 onreadystatechange( "timeout" );
454 xhr.send( type === "POST" ? s.data : null );
456 jQuery.handleError(s, xhr, null, e);
459 // firefox 1.5 doesn't fire statechange for sync requests
461 onreadystatechange();
465 // If a local callback was specified, fire it and pass it the data
467 s.success( data, status );
470 // Fire the global callback
472 jQuery.event.trigger( "ajaxSuccess", [xhr, s] );
479 s.complete(xhr, status);
482 // The request was completed
484 jQuery.event.trigger( "ajaxComplete", [xhr, s] );
487 // Handle the global AJAX counter
488 if ( s.global && ! --jQuery.active ) {
489 jQuery.event.trigger( "ajaxStop" );
493 // return XMLHttpRequest to allow aborting the request etc.
497 handleError: function( s, xhr, status, e ) {
498 // If a local callback was specified, fire it
500 s.error( xhr, status, e );
503 // Fire the global callback
505 jQuery.event.trigger( "ajaxError", [xhr, s, e] );
509 // Counter for holding the number of active queries
512 // Determines if an XMLHttpRequest was successful or not
513 httpSuccess: function( xhr ) {
515 // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
516 return !xhr.status && location.protocol === "file:" ||
517 // Opera returns 0 when status is 304
518 ( xhr.status >= 200 && xhr.status < 300 ) ||
519 xhr.status === 304 || xhr.status === 1223 || xhr.status === 0;
525 // Determines if an XMLHttpRequest returns NotModified
526 httpNotModified: function( xhr, url ) {
527 var last_modified = xhr.getResponseHeader("Last-Modified"),
528 etag = xhr.getResponseHeader("Etag");
530 if ( last_modified ) {
531 jQuery.lastModified[url] = last_modified;
535 jQuery.etag[url] = etag;
538 // Opera returns 0 when status is 304
539 return xhr.status === 304 || xhr.status === 0;
542 httpData: function( xhr, type, s ) {
543 var ct = xhr.getResponseHeader("content-type"),
544 xml = type === "xml" || !type && ct && ct.indexOf("xml") >= 0,
545 data = xml ? xhr.responseXML : xhr.responseText;
547 if ( xml && data.documentElement.nodeName === "parsererror" ) {
551 // Allow a pre-filtering function to sanitize the response
552 // s is checked to keep backwards compatibility
553 if ( s && s.dataFilter ) {
554 data = s.dataFilter( data, type );
557 // The filter can actually parse the response
558 if ( typeof data === "string" ) {
560 // If the type is "script", eval it in global context
561 if ( type === "script" ) {
562 jQuery.globalEval( data );
565 // Get the JavaScript object, if JSON is used.
566 if ( type === "json" ) {
567 if ( typeof JSON === "object" && JSON.parse ) {
568 data = JSON.parse( data );
570 data = (new Function("return " + data))();
578 // Serialize an array of form elements or a set of
579 // key/values into a query string
580 param: function( a ) {
583 function add( key, value ){
584 s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
587 // If an array was passed in, assume that it is an array
589 if ( jQuery.isArray(a) || a.jquery ) {
590 // Serialize the form elements
591 jQuery.each( a, function(){
592 add( this.name, this.value );
595 // Otherwise, assume that it's an object of key/value pairs
597 // Serialize the key/values
599 // If the value is an array then the key names need to be repeated
600 if ( jQuery.isArray(a[j]) ) {
601 jQuery.each( a[j], function(){
605 add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );
610 // Return the resulting serialization
611 return s.join("&").replace(r20, "+");