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 );
70 // Attach a bunch of functions for handling common AJAX events
71 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
72 jQuery.fn[o] = function(f){
73 return this.bind(o, f);
77 var jsc = (new Date).getTime();
80 get: function( url, data, callback, type ) {
81 // shift arguments if data argument was ommited
82 if ( jQuery.isFunction( data ) ) {
96 getScript: function( url, callback ) {
97 return jQuery.get(url, null, callback, "script");
100 getJSON: function( url, data, callback ) {
101 return jQuery.get(url, data, callback, "json");
104 post: function( url, data, callback, type ) {
105 if ( jQuery.isFunction( data ) ) {
119 ajaxSetup: function( settings ) {
120 jQuery.extend( jQuery.ajaxSettings, settings );
127 contentType: "application/x-www-form-urlencoded",
133 // Last-Modified header cache for next request
136 ajax: function( s ) {
137 var jsonp, jsre = /=(\?|%3F)/g, status, data;
139 // Extend the settings, but re-extend 's' so that it can be
140 // checked again later (in the test suite, specifically)
141 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
143 // convert data if not already a string
144 if ( s.data && s.processData && typeof s.data != "string" )
145 s.data = jQuery.param(s.data);
147 // Break the data into one single string
148 var q = s.url.indexOf("?");
150 s.data = (s.data ? s.data + "&" : "") + s.url.slice(q + 1);
151 s.url = s.url.slice(0, q);
154 // Handle JSONP Parameter Callbacks
155 if ( s.dataType == "jsonp" ) {
156 if ( !s.data || !s.data.match(jsre) )
157 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
161 // Build temporary JSONP function
162 if ( s.dataType == "json" && s.data && s.data.match(jsre) ) {
163 jsonp = "jsonp" + jsc++;
164 s.data = s.data.replace(jsre, "=" + jsonp);
166 // We need to make sure
167 // that a JSONP style response is executed properly
168 s.dataType = "script";
170 // Handle JSONP-style loading
171 window[ jsonp ] = function(tmp){
175 window[ jsonp ] = undefined;
176 try{ delete window[ jsonp ]; } catch(e){}
180 if ( s.dataType == "script" && s.cache == null )
183 if ( s.cache === false && s.type.toLowerCase() == "get" )
184 s.data = (s.data ? s.data + "&" : "") + "_=" + (new Date()).getTime();
186 // If data is available, append data to url for get requests
187 if ( s.data && s.type.toLowerCase() == "get" ) {
188 s.url += "?" + s.data;
190 // IE likes to send both get and post data, prevent this
194 // Watch for a new set of requests
195 if ( s.global && ! jQuery.active++ )
196 jQuery.event.trigger( "ajaxStart" );
198 // If we're requesting a remote document
199 // and trying to load JSON or Script
200 if ( !s.url.indexOf("http") && s.dataType == "script" ) {
201 var script = document.createElement("script");
204 // Handle Script loading
205 if ( !jsonp && (s.success || s.complete) ) {
208 // Attach handlers for all browsers
209 script.onload = script.onreadystatechange = function(){
210 if ( !done && (!this.readyState ||
211 this.readyState == "loaded" || this.readyState == "complete") ) {
215 document.body.removeChild( script );
220 document.body.appendChild(script);
222 // We handle everything using the script element injection
226 var requestDone = false;
228 // Create the request object; Microsoft failed to properly
229 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
230 var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
233 xml.open(s.type, s.url, s.async);
235 // Set the correct header, if data is being sent
237 xml.setRequestHeader("Content-Type", s.contentType);
239 // Set the If-Modified-Since header, if ifModified mode.
241 xml.setRequestHeader("If-Modified-Since",
242 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
244 // Set header so the called script knows that it's an XMLHttpRequest
245 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
247 // Allow custom headers/mimetypes
252 jQuery.event.trigger("ajaxSend", [xml, s]);
254 // Wait for a response to come back
255 var onreadystatechange = function(isTimeout){
256 // The transfer is complete and the data is available, or the request timed out
257 if ( !requestDone && xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
260 // clear poll interval
266 status = isTimeout == "timeout" && "timeout" ||
267 !jQuery.httpSuccess( xml ) && "error" ||
268 s.ifModified && jQuery.httpNotModified( xml, s.url ) && "notmodified" ||
271 if ( status == "success" ) {
272 // Watch for, and catch, XML document parse errors
274 // process the data (runs the xml through httpData regardless of callback)
275 data = jQuery.httpData( xml, s.dataType );
277 status = "parsererror";
281 // Make sure that the request was successful or notmodified
282 if ( status == "success" ) {
283 // Cache Last-Modified header, if ifModified mode.
286 modRes = xml.getResponseHeader("Last-Modified");
287 } catch(e) {} // swallow exception thrown by FF if header is not available
289 if ( s.ifModified && modRes )
290 jQuery.lastModified[s.url] = modRes;
292 // JSONP handles its own success callback
296 jQuery.handleError(s, xml, status);
298 // Fire the complete handlers
308 // don't attach the handler to the request, just poll it instead
309 var ival = setInterval(onreadystatechange, 13);
313 setTimeout(function(){
314 // Check to see if the request is still happening
316 // Cancel the request
320 onreadystatechange( "timeout" );
329 jQuery.handleError(s, xml, null, e);
332 // firefox 1.5 doesn't fire statechange for sync requests
334 onreadystatechange();
336 // return XMLHttpRequest to allow aborting the request etc.
340 // If a local callback was specified, fire it and pass it the data
342 s.success( data, status );
344 // Fire the global callback
346 jQuery.event.trigger( "ajaxSuccess", [xml, s] );
352 s.complete(xml, status);
354 // The request was completed
356 jQuery.event.trigger( "ajaxComplete", [xml, s] );
358 // Handle the global AJAX counter
359 if ( s.global && ! --jQuery.active )
360 jQuery.event.trigger( "ajaxStop" );
364 handleError: function( s, xml, status, e ) {
365 // If a local callback was specified, fire it
366 if ( s.error ) s.error( xml, status, e );
368 // Fire the global callback
370 jQuery.event.trigger( "ajaxError", [xml, s, e] );
373 // Counter for holding the number of active queries
376 // Determines if an XMLHttpRequest was successful or not
377 httpSuccess: function( r ) {
379 return !r.status && location.protocol == "file:" ||
380 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
381 jQuery.browser.safari && r.status == undefined;
386 // Determines if an XMLHttpRequest returns NotModified
387 httpNotModified: function( xml, url ) {
389 var xmlRes = xml.getResponseHeader("Last-Modified");
391 // Firefox always returns 200. check Last-Modified date
392 return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
393 jQuery.browser.safari && xml.status == undefined;
398 httpData: function( r, type ) {
399 var ct = r.getResponseHeader("content-type");
400 var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0;
401 var data = xml ? r.responseXML : r.responseText;
403 if ( xml && data.documentElement.tagName == "parsererror" )
406 // If the type is "script", eval it in global context
407 if ( type == "script" )
408 jQuery.globalEval( data );
410 // Get the JavaScript object, if JSON is used.
411 if ( type == "json" )
412 data = eval("(" + data + ")");
417 // Serialize an array of form elements or a set of
418 // key/values into a query string
419 param: function( a ) {
422 // If an array was passed in, assume that it is an array
424 if ( a.constructor == Array || a.jquery )
425 // Serialize the form elements
426 jQuery.each( a, function(){
427 s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
430 // Otherwise, assume that it's an object of key/value pairs
432 // Serialize the key/values
434 // If the value is an array then the key names need to be repeated
435 if ( a[j] && a[j].constructor == Array )
436 jQuery.each( a[j], function(){
437 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
440 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
442 // Return the resulting serialization