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 // Add delay to account for Safari's delay in globalEval
56 setTimeout(function(){
57 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 jQuery.nodeName(this, "form") ?
70 jQuery.makeArray(this.elements) : this;
73 return this.name && !this.disabled &&
74 (this.checked || /select|textarea/i.test(this.nodeName) ||
75 /text|hidden|password/i.test(this.type));
77 .map(function(i, elem){
78 var val = jQuery(this).val();
79 return val == null ? null :
80 val.constructor == Array ?
81 jQuery.map( val, function(val, i){
82 return {name: elem.name, value: val};
84 {name: elem.name, value: val};
89 // Attach a bunch of functions for handling common AJAX events
90 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
91 jQuery.fn[o] = function(f){
92 return this.bind(o, f);
96 var jsc = (new Date).getTime();
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 );
146 contentType: "application/x-www-form-urlencoded",
152 // Last-Modified header cache for next request
155 ajax: function( s ) {
156 var jsonp, jsre = /=(\?|%3F)/g, status, data;
158 // Extend the settings, but re-extend 's' so that it can be
159 // checked again later (in the test suite, specifically)
160 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
162 // convert data if not already a string
163 if ( s.data && s.processData && typeof s.data != "string" )
164 s.data = jQuery.param(s.data);
166 // Handle JSONP Parameter Callbacks
167 if ( s.dataType == "jsonp" ) {
168 if ( s.type.toLowerCase() == "get" ) {
169 if ( !s.url.match(jsre) )
170 s.url += (s.url.match(/\?/) ? "&" : "?") + (s.jsonp || "callback") + "=?";
171 } else if ( !s.data || !s.data.match(jsre) )
172 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
176 // Build temporary JSONP function
177 if ( s.dataType == "json" && (s.data && s.data.match(jsre) || s.url.match(jsre)) ) {
178 jsonp = "jsonp" + jsc++;
180 // Replace the =? sequence both in the query string and the data
182 s.data = s.data.replace(jsre, "=" + jsonp);
183 s.url = s.url.replace(jsre, "=" + jsonp);
185 // We need to make sure
186 // that a JSONP style response is executed properly
187 s.dataType = "script";
189 // Handle JSONP-style loading
190 window[ jsonp ] = function(tmp){
195 window[ jsonp ] = undefined;
196 try{ delete window[ jsonp ]; } catch(e){}
200 if ( s.dataType == "script" && s.cache == null )
203 if ( s.cache === false && s.type.toLowerCase() == "get" )
204 s.url += (s.url.match(/\?/) ? "&" : "?") + "_=" + (new Date()).getTime();
206 // If data is available, append data to url for get requests
207 if ( s.data && s.type.toLowerCase() == "get" ) {
208 s.url += (s.url.match(/\?/) ? "&" : "?") + s.data;
210 // IE likes to send both get and post data, prevent this
214 // Watch for a new set of requests
215 if ( s.global && ! jQuery.active++ )
216 jQuery.event.trigger( "ajaxStart" );
218 // If we're requesting a remote document
219 // and trying to load JSON or Script
220 if ( !s.url.indexOf("http") && s.dataType == "script" ) {
221 var head = document.getElementsByTagName("head")[0];
222 var script = document.createElement("script");
225 // Handle Script loading
226 if ( !jsonp && (s.success || s.complete) ) {
229 // Attach handlers for all browsers
230 script.onload = script.onreadystatechange = function(){
231 if ( !done && (!this.readyState ||
232 this.readyState == "loaded" || this.readyState == "complete") ) {
236 head.removeChild( script );
241 head.appendChild(script);
243 // We handle everything using the script element injection
247 var requestDone = false;
249 // Create the request object; Microsoft failed to properly
250 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
251 var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
254 xml.open(s.type, s.url, s.async);
256 // Set the correct header, if data is being sent
258 xml.setRequestHeader("Content-Type", s.contentType);
260 // Set the If-Modified-Since header, if ifModified mode.
262 xml.setRequestHeader("If-Modified-Since",
263 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
265 // Set header so the called script knows that it's an XMLHttpRequest
266 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
268 // Allow custom headers/mimetypes
273 jQuery.event.trigger("ajaxSend", [xml, s]);
275 // Wait for a response to come back
276 var onreadystatechange = function(isTimeout){
277 // The transfer is complete and the data is available, or the request timed out
278 if ( !requestDone && xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
281 // clear poll interval
287 status = isTimeout == "timeout" && "timeout" ||
288 !jQuery.httpSuccess( xml ) && "error" ||
289 s.ifModified && jQuery.httpNotModified( xml, s.url ) && "notmodified" ||
292 if ( status == "success" ) {
293 // Watch for, and catch, XML document parse errors
295 // process the data (runs the xml through httpData regardless of callback)
296 data = jQuery.httpData( xml, s.dataType );
298 status = "parsererror";
302 // Make sure that the request was successful or notmodified
303 if ( status == "success" ) {
304 // Cache Last-Modified header, if ifModified mode.
307 modRes = xml.getResponseHeader("Last-Modified");
308 } catch(e) {} // swallow exception thrown by FF if header is not available
310 if ( s.ifModified && modRes )
311 jQuery.lastModified[s.url] = modRes;
313 // JSONP handles its own success callback
317 jQuery.handleError(s, xml, status);
319 // Fire the complete handlers
329 // don't attach the handler to the request, just poll it instead
330 var ival = setInterval(onreadystatechange, 13);
334 setTimeout(function(){
335 // Check to see if the request is still happening
337 // Cancel the request
341 onreadystatechange( "timeout" );
350 jQuery.handleError(s, xml, null, e);
353 // firefox 1.5 doesn't fire statechange for sync requests
355 onreadystatechange();
357 // return XMLHttpRequest to allow aborting the request etc.
361 // If a local callback was specified, fire it and pass it the data
363 s.success( data, status );
365 // Fire the global callback
367 jQuery.event.trigger( "ajaxSuccess", [xml, s] );
373 s.complete(xml, status);
375 // The request was completed
377 jQuery.event.trigger( "ajaxComplete", [xml, s] );
379 // Handle the global AJAX counter
380 if ( s.global && ! --jQuery.active )
381 jQuery.event.trigger( "ajaxStop" );
385 handleError: function( s, xml, status, e ) {
386 // If a local callback was specified, fire it
387 if ( s.error ) s.error( xml, status, e );
389 // Fire the global callback
391 jQuery.event.trigger( "ajaxError", [xml, s, e] );
394 // Counter for holding the number of active queries
397 // Determines if an XMLHttpRequest was successful or not
398 httpSuccess: function( r ) {
400 return !r.status && location.protocol == "file:" ||
401 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
402 jQuery.browser.safari && r.status == undefined;
407 // Determines if an XMLHttpRequest returns NotModified
408 httpNotModified: function( xml, url ) {
410 var xmlRes = xml.getResponseHeader("Last-Modified");
412 // Firefox always returns 200. check Last-Modified date
413 return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
414 jQuery.browser.safari && xml.status == undefined;
419 httpData: function( r, type ) {
420 var ct = r.getResponseHeader("content-type");
421 var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0;
422 var data = xml ? r.responseXML : r.responseText;
424 if ( xml && data.documentElement.tagName == "parsererror" )
427 // If the type is "script", eval it in global context
428 if ( type == "script" )
429 jQuery.globalEval( data );
431 // Get the JavaScript object, if JSON is used.
432 if ( type == "json" )
433 data = eval("(" + data + ")");
438 // Serialize an array of form elements or a set of
439 // key/values into a query string
440 param: function( a ) {
443 // If an array was passed in, assume that it is an array
445 if ( a.constructor == Array || a.jquery )
446 // Serialize the form elements
447 jQuery.each( a, function(){
448 s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
451 // Otherwise, assume that it's an object of key/value pairs
453 // Serialize the key/values
455 // If the value is an array then the key names need to be repeated
456 if ( a[j] && a[j].constructor == Array )
457 jQuery.each( a[j], function(){
458 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
461 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
463 // Return the resulting serialization
464 return s.join("&").replace(/%20/g, "+");