2 load: function( url, params, callback ) {
3 if ( jQuery.isFunction( url ) )
4 return this.bind("load", url);
6 var off = url.indexOf(" ");
8 var selector = url.slice(off, url.length);
9 url = url.slice(0, off);
12 callback = callback || function(){};
14 // Default to a GET request
17 // If the second parameter was provided
20 if ( jQuery.isFunction( params ) ) {
21 // We assume that it's the callback
25 // Otherwise, build a param string
27 params = jQuery.param( params );
33 // Request the remote document
38 complete: function(res, status){
39 // If successful, inject the HTML into all the matched elements
40 if ( status == "success" || status == "notmodified" )
41 // See if a selector was specified
43 // Create a dummy div to hold the results
45 // inject the contents of the document in, removing the scripts
46 // to avoid any 'Permission Denied' errors in IE
47 .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))
49 // Locate the specified elements
52 // If not, just inject the full result
55 self.each( callback, [res.responseText, status, res] );
61 serialize: function() {
62 return jQuery.param(this.serializeArray());
64 serializeArray: function() {
65 return this.map(function(){
66 return jQuery.nodeName(this, "form") ?
67 jQuery.makeArray(this.elements) : this;
70 return this.name && !this.disabled &&
71 (this.checked || /select|textarea/i.test(this.nodeName) ||
72 /text|hidden|password/i.test(this.type));
74 .map(function(i, elem){
75 var val = jQuery(this).val();
76 return val == null ? null :
77 val.constructor == Array ?
78 jQuery.map( val, function(val, i){
79 return {name: elem.name, value: val};
81 {name: elem.name, value: val};
86 // Attach a bunch of functions for handling common AJAX events
87 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
88 jQuery.fn[o] = function(f){
89 return this.bind(o, f);
93 var jsc = (new Date).getTime();
96 get: function( url, data, callback, type ) {
97 // shift arguments if data argument was ommited
98 if ( jQuery.isFunction( data ) ) {
112 getScript: function( url, callback ) {
113 return jQuery.get(url, null, callback, "script");
116 getJSON: function( url, data, callback ) {
117 return jQuery.get(url, data, callback, "json");
120 post: function( url, data, callback, type ) {
121 if ( jQuery.isFunction( data ) ) {
135 ajaxSetup: function( settings ) {
136 jQuery.extend( jQuery.ajaxSettings, settings );
143 contentType: "application/x-www-form-urlencoded",
149 // Last-Modified header cache for next request
152 ajax: function( s ) {
153 var jsonp, jsre = /=(\?|%3F)/g, status, data;
155 // Extend the settings, but re-extend 's' so that it can be
156 // checked again later (in the test suite, specifically)
157 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
159 // convert data if not already a string
160 if ( s.data && s.processData && typeof s.data != "string" )
161 s.data = jQuery.param(s.data);
163 // Handle JSONP Parameter Callbacks
164 if ( s.dataType == "jsonp" ) {
165 if ( s.type.toLowerCase() == "get" ) {
166 if ( !s.url.match(jsre) )
167 s.url += (s.url.match(/\?/) ? "&" : "?") + (s.jsonp || "callback") + "=?";
168 } else if ( !s.data || !s.data.match(jsre) )
169 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
173 // Build temporary JSONP function
174 if ( s.dataType == "json" && (s.data && s.data.match(jsre) || s.url.match(jsre)) ) {
175 jsonp = "jsonp" + jsc++;
177 // Replace the =? sequence both in the query string and the data
179 s.data = (s.data + "").replace(jsre, "=" + jsonp);
180 s.url = s.url.replace(jsre, "=" + jsonp);
182 // We need to make sure
183 // that a JSONP style response is executed properly
184 s.dataType = "script";
186 // Handle JSONP-style loading
187 window[ jsonp ] = function(tmp){
192 window[ jsonp ] = undefined;
193 try{ delete window[ jsonp ]; } catch(e){}
197 if ( s.dataType == "script" && s.cache == null )
200 if ( s.cache === false && s.type.toLowerCase() == "get" )
201 s.url += (s.url.match(/\?/) ? "&" : "?") + "_=" + (new Date()).getTime();
203 // If data is available, append data to url for get requests
204 if ( s.data && s.type.toLowerCase() == "get" ) {
205 s.url += (s.url.match(/\?/) ? "&" : "?") + s.data;
207 // IE likes to send both get and post data, prevent this
211 // Watch for a new set of requests
212 if ( s.global && ! jQuery.active++ )
213 jQuery.event.trigger( "ajaxStart" );
215 // If we're requesting a remote document
216 // and trying to load JSON or Script
217 if ( !s.url.indexOf("http") && ( s.dataType == "script" || s.dataType =="json" ) ) {
218 var head = document.getElementsByTagName("head")[0];
219 var script = document.createElement("script");
222 // Handle Script loading
226 // Attach handlers for all browsers
227 script.onload = script.onreadystatechange = function(){
228 if ( !done && (!this.readyState ||
229 this.readyState == "loaded" || this.readyState == "complete") ) {
233 head.removeChild( script );
238 head.appendChild(script);
240 // We handle everything using the script element injection
244 var requestDone = false;
246 // Create the request object; Microsoft failed to properly
247 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
248 var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
251 xml.open(s.type, s.url, s.async);
253 // Set the correct header, if data is being sent
255 xml.setRequestHeader("Content-Type", s.contentType);
257 // Set the If-Modified-Since header, if ifModified mode.
259 xml.setRequestHeader("If-Modified-Since",
260 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
262 // Set header so the called script knows that it's an XMLHttpRequest
263 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
265 // Allow custom headers/mimetypes
270 jQuery.event.trigger("ajaxSend", [xml, s]);
272 // Wait for a response to come back
273 var onreadystatechange = function(isTimeout){
274 // The transfer is complete and the data is available, or the request timed out
275 if ( !requestDone && xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
278 // clear poll interval
284 status = isTimeout == "timeout" && "timeout" ||
285 !jQuery.httpSuccess( xml ) && "error" ||
286 s.ifModified && jQuery.httpNotModified( xml, s.url ) && "notmodified" ||
289 if ( status == "success" ) {
290 // Watch for, and catch, XML document parse errors
292 // process the data (runs the xml through httpData regardless of callback)
293 data = jQuery.httpData( xml, s.dataType );
295 status = "parsererror";
299 // Make sure that the request was successful or notmodified
300 if ( status == "success" ) {
301 // Cache Last-Modified header, if ifModified mode.
304 modRes = xml.getResponseHeader("Last-Modified");
305 } catch(e) {} // swallow exception thrown by FF if header is not available
307 if ( s.ifModified && modRes )
308 jQuery.lastModified[s.url] = modRes;
310 // JSONP handles its own success callback
314 jQuery.handleError(s, xml, status);
316 // Fire the complete handlers
326 // don't attach the handler to the request, just poll it instead
327 var ival = setInterval(onreadystatechange, 13);
331 setTimeout(function(){
332 // Check to see if the request is still happening
334 // Cancel the request
338 onreadystatechange( "timeout" );
347 jQuery.handleError(s, xml, null, e);
350 // firefox 1.5 doesn't fire statechange for sync requests
352 onreadystatechange();
354 // return XMLHttpRequest to allow aborting the request etc.
358 // If a local callback was specified, fire it and pass it the data
360 s.success( data, status );
362 // Fire the global callback
364 jQuery.event.trigger( "ajaxSuccess", [xml, s] );
370 s.complete(xml, status);
372 // The request was completed
374 jQuery.event.trigger( "ajaxComplete", [xml, s] );
376 // Handle the global AJAX counter
377 if ( s.global && ! --jQuery.active )
378 jQuery.event.trigger( "ajaxStop" );
382 handleError: function( s, xml, status, e ) {
383 // If a local callback was specified, fire it
384 if ( s.error ) s.error( xml, status, e );
386 // Fire the global callback
388 jQuery.event.trigger( "ajaxError", [xml, s, e] );
391 // Counter for holding the number of active queries
394 // Determines if an XMLHttpRequest was successful or not
395 httpSuccess: function( r ) {
397 // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
398 return !r.status && location.protocol == "file:" ||
399 ( r.status >= 200 && r.status < 300 ) || r.status == 304 || r.status == 1223 ||
400 jQuery.browser.safari && r.status == undefined;
405 // Determines if an XMLHttpRequest returns NotModified
406 httpNotModified: function( xml, url ) {
408 var xmlRes = xml.getResponseHeader("Last-Modified");
410 // Firefox always returns 200. check Last-Modified date
411 return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
412 jQuery.browser.safari && xml.status == undefined;
417 httpData: function( r, type ) {
418 var ct = r.getResponseHeader("content-type");
419 var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0;
420 var data = xml ? r.responseXML : r.responseText;
422 if ( xml && data.documentElement.tagName == "parsererror" )
425 // If the type is "script", eval it in global context
426 if ( type == "script" )
427 jQuery.globalEval( data );
429 // Get the JavaScript object, if JSON is used.
430 if ( type == "json" )
431 data = eval("(" + data + ")");
436 // Serialize an array of form elements or a set of
437 // key/values into a query string
438 param: function( a ) {
441 // If an array was passed in, assume that it is an array
443 if ( a.constructor == Array || a.jquery )
444 // Serialize the form elements
445 jQuery.each( a, function(){
446 s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
449 // Otherwise, assume that it's an object of key/value pairs
451 // Serialize the key/values
453 // If the value is an array then the key names need to be repeated
454 if ( a[j] && a[j].constructor == Array )
455 jQuery.each( a[j], function(){
456 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
459 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
461 // Return the resulting serialization
462 return s.join("&").replace(/%20/g, "+");