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 callback = callback || function(){};
17 // Default to a GET request
20 // If the second parameter was provided
23 if ( jQuery.isFunction( params ) ) {
24 // We assume that it's the callback
28 // Otherwise, build a param string
30 params = jQuery.param( params );
36 // Request the remote document
42 complete: function(res, status){
43 // If successful, inject the HTML into all the matched elements
44 if ( status == "success" || status == "notmodified" )
45 // See if a selector was specified
47 // Create a dummy div to hold the results
49 // inject the contents of the document in, removing the scripts
50 // to avoid any 'Permission Denied' errors in IE
51 .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))
53 // Locate the specified elements
56 // If not, just inject the full result
59 self.each( callback, [res.responseText, status, res] );
65 serialize: function() {
66 return jQuery.param(this.serializeArray());
68 serializeArray: function() {
69 return this.map(function(){
70 return jQuery.nodeName(this, "form") ?
71 jQuery.makeArray(this.elements) : this;
74 return this.name && !this.disabled &&
75 (this.checked || /select|textarea/i.test(this.nodeName) ||
76 /text|hidden|password/i.test(this.type));
78 .map(function(i, elem){
79 var val = jQuery(this).val();
80 return val == null ? null :
81 val.constructor == Array ?
82 jQuery.map( val, function(val, i){
83 return {name: elem.name, value: val};
85 {name: elem.name, value: val};
90 // Attach a bunch of functions for handling common AJAX events
91 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
92 jQuery.fn[o] = function(f){
93 return this.bind(o, f);
100 get: function( url, data, callback, type ) {
101 // shift arguments if data argument was ommited
102 if ( jQuery.isFunction( data ) ) {
116 getScript: function( url, callback ) {
117 return jQuery.get(url, null, callback, "script");
120 getJSON: function( url, data, callback ) {
121 return jQuery.get(url, data, callback, "json");
124 post: function( url, data, callback, type ) {
125 if ( jQuery.isFunction( data ) ) {
139 ajaxSetup: function( settings ) {
140 jQuery.extend( jQuery.ajaxSettings, settings );
148 contentType: "application/x-www-form-urlencoded",
155 xml: "application/xml, text/xml",
157 script: "text/javascript, application/javascript",
158 json: "application/json, text/javascript",
164 // Last-Modified header cache for next request
167 ajax: function( s ) {
168 var jsonp, jsre = /=\?(&|$)/g, status, data;
170 // Extend the settings, but re-extend 's' so that it can be
171 // checked again later (in the test suite, specifically)
172 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
174 // convert data if not already a string
175 if ( s.data && s.processData && typeof s.data != "string" )
176 s.data = jQuery.param(s.data);
178 // Handle JSONP Parameter Callbacks
179 if ( s.dataType == "jsonp" ) {
180 if ( s.type.toLowerCase() == "get" ) {
181 if ( !s.url.match(jsre) )
182 s.url += (s.url.match(/\?/) ? "&" : "?") + (s.jsonp || "callback") + "=?";
183 } else if ( !s.data || !s.data.match(jsre) )
184 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
188 // Build temporary JSONP function
189 if ( s.dataType == "json" && (s.data && s.data.match(jsre) || s.url.match(jsre)) ) {
190 jsonp = "jsonp" + jsc++;
192 // Replace the =? sequence both in the query string and the data
194 s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
195 s.url = s.url.replace(jsre, "=" + jsonp + "$1");
197 // We need to make sure
198 // that a JSONP style response is executed properly
199 s.dataType = "script";
201 // Handle JSONP-style loading
202 window[ jsonp ] = function(tmp){
207 window[ jsonp ] = undefined;
208 try{ delete window[ jsonp ]; } catch(e){}
210 head.removeChild( script );
214 if ( s.dataType == "script" && s.cache == null )
217 if ( s.cache === false && s.type.toLowerCase() == "get" ) {
219 // try replacing _= if it is there
220 var ret = s.url.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2");
221 // if nothing was replaced, add timestamp to the end
222 s.url = ret + ((ret == s.url) ? (s.url.match(/\?/) ? "&" : "?") + "_=" + ts : "");
225 // If data is available, append data to url for get requests
226 if ( s.data && s.type.toLowerCase() == "get" ) {
227 s.url += (s.url.match(/\?/) ? "&" : "?") + s.data;
229 // IE likes to send both get and post data, prevent this
233 // Watch for a new set of requests
234 if ( s.global && ! jQuery.active++ )
235 jQuery.event.trigger( "ajaxStart" );
237 // Matches an absolute URL, and saves the domain
238 var remote = /^(?:\w+:)?\/\/([^\/?#]+)/;
240 // If we're requesting a remote document
241 // and trying to load JSON or Script with a GET
242 if ( s.dataType == "script" && s.type.toLowerCase() == "get"
243 && remote.test(s.url) && remote.exec(s.url)[1] != location.host ){
244 var head = document.getElementsByTagName("head")[0];
245 var script = document.createElement("script");
248 script.charset = s.scriptCharset;
250 // Handle Script loading
254 // Attach handlers for all browsers
255 script.onload = script.onreadystatechange = function(){
256 if ( !done && (!this.readyState ||
257 this.readyState == "loaded" || this.readyState == "complete") ) {
261 head.removeChild( script );
266 head.appendChild(script);
268 // We handle everything using the script element injection
272 var requestDone = false;
274 // Create the request object; Microsoft failed to properly
275 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
276 var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
279 // Passing null username, generates a login popup on Opera (#2865)
281 xml.open(s.type, s.url, s.async, s.username, s.password);
283 xml.open(s.type, s.url, s.async);
285 // Need an extra try/catch for cross domain requests in Firefox 3
287 // Set the correct header, if data is being sent
289 xml.setRequestHeader("Content-Type", s.contentType);
291 // Set the If-Modified-Since header, if ifModified mode.
293 xml.setRequestHeader("If-Modified-Since",
294 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
296 // Set header so the called script knows that it's an XMLHttpRequest
297 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
299 // Set the Accepts header for the server, depending on the dataType
300 xml.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
301 s.accepts[ s.dataType ] + ", */*" :
302 s.accepts._default );
305 // Allow custom headers/mimetypes
306 if ( s.beforeSend && s.beforeSend(xml, s) === false ) {
307 // cleanup active request counter
308 s.global && jQuery.active--;
309 // close opended socket
315 jQuery.event.trigger("ajaxSend", [xml, s]);
317 // Wait for a response to come back
318 var onreadystatechange = function(isTimeout){
319 // The transfer is complete and the data is available, or the request timed out
320 if ( !requestDone && xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
323 // clear poll interval
329 status = isTimeout == "timeout" && "timeout" ||
330 !jQuery.httpSuccess( xml ) && "error" ||
331 s.ifModified && jQuery.httpNotModified( xml, s.url ) && "notmodified" ||
334 if ( status == "success" ) {
335 // Watch for, and catch, XML document parse errors
337 // process the data (runs the xml through httpData regardless of callback)
338 data = jQuery.httpData( xml, s.dataType );
340 status = "parsererror";
344 // Make sure that the request was successful or notmodified
345 if ( status == "success" ) {
346 // Cache Last-Modified header, if ifModified mode.
349 modRes = xml.getResponseHeader("Last-Modified");
350 } catch(e) {} // swallow exception thrown by FF if header is not available
352 if ( s.ifModified && modRes )
353 jQuery.lastModified[s.url] = modRes;
355 // JSONP handles its own success callback
359 jQuery.handleError(s, xml, status);
361 // Fire the complete handlers
371 // don't attach the handler to the request, just poll it instead
372 var ival = setInterval(onreadystatechange, 13);
376 setTimeout(function(){
377 // Check to see if the request is still happening
379 // Cancel the request
383 onreadystatechange( "timeout" );
392 jQuery.handleError(s, xml, null, e);
395 // firefox 1.5 doesn't fire statechange for sync requests
397 onreadystatechange();
400 // If a local callback was specified, fire it and pass it the data
402 s.success( data, status );
404 // Fire the global callback
406 jQuery.event.trigger( "ajaxSuccess", [xml, s] );
412 s.complete(xml, status);
414 // The request was completed
416 jQuery.event.trigger( "ajaxComplete", [xml, s] );
418 // Handle the global AJAX counter
419 if ( s.global && ! --jQuery.active )
420 jQuery.event.trigger( "ajaxStop" );
423 // return XMLHttpRequest to allow aborting the request etc.
427 handleError: function( s, xml, status, e ) {
428 // If a local callback was specified, fire it
429 if ( s.error ) s.error( xml, status, e );
431 // Fire the global callback
433 jQuery.event.trigger( "ajaxError", [xml, s, e] );
436 // Counter for holding the number of active queries
439 // Determines if an XMLHttpRequest was successful or not
440 httpSuccess: function( r ) {
442 // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
443 return !r.status && location.protocol == "file:" ||
444 ( r.status >= 200 && r.status < 300 ) || r.status == 304 || r.status == 1223 ||
445 jQuery.browser.safari && r.status == undefined;
450 // Determines if an XMLHttpRequest returns NotModified
451 httpNotModified: function( xml, url ) {
453 var xmlRes = xml.getResponseHeader("Last-Modified");
455 // Firefox always returns 200. check Last-Modified date
456 return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
457 jQuery.browser.safari && xml.status == undefined;
462 httpData: function( r, type ) {
463 var ct = r.getResponseHeader("content-type"),
464 xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
465 data = xml ? r.responseXML : r.responseText;
467 if ( xml && data.documentElement.tagName == "parsererror" )
470 // If the type is "script", eval it in global context
471 if ( type == "script" )
472 jQuery.globalEval( data );
474 // Get the JavaScript object, if JSON is used.
475 if ( type == "json" )
476 data = eval("(" + data + ")");
481 // Serialize an array of form elements or a set of
482 // key/values into a query string
483 param: function( a ) {
486 // If an array was passed in, assume that it is an array
488 if ( a.constructor == Array || a.jquery )
489 // Serialize the form elements
490 jQuery.each( a, function(){
491 s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
494 // Otherwise, assume that it's an object of key/value pairs
496 // Serialize the key/values
498 // If the value is an array then the key names need to be repeated
499 if ( a[j] && a[j].constructor == Array )
500 jQuery.each( a[j], function(){
501 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
504 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( jQuery.isFunction(a[j]) ? a[j]() : a[j] ) );
506 // Return the resulting serialization
507 return s.join("&").replace(/%20/g, "+");