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(i, val){
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 // Break the data into one single string
167 var q = s.url.indexOf("?");
169 s.data = (s.data ? s.data + "&" : "") + s.url.slice(q + 1);
170 s.url = s.url.slice(0, q);
173 // Handle JSONP Parameter Callbacks
174 if ( s.dataType == "jsonp" ) {
175 if ( !s.data || !s.data.match(jsre) )
176 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
180 // Build temporary JSONP function
181 if ( s.dataType == "json" && s.data && s.data.match(jsre) ) {
182 jsonp = "jsonp" + jsc++;
183 s.data = s.data.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){
194 window[ jsonp ] = undefined;
195 try{ delete window[ jsonp ]; } catch(e){}
199 if ( s.dataType == "script" && s.cache == null )
202 if ( s.cache === false && s.type.toLowerCase() == "get" )
203 s.data = (s.data ? s.data + "&" : "") + "_=" + (new Date()).getTime();
205 // If data is available, append data to url for get requests
206 if ( s.data && s.type.toLowerCase() == "get" ) {
207 s.url += "?" + s.data;
209 // IE likes to send both get and post data, prevent this
213 // Watch for a new set of requests
214 if ( s.global && ! jQuery.active++ )
215 jQuery.event.trigger( "ajaxStart" );
217 // If we're requesting a remote document
218 // and trying to load JSON or Script
219 if ( !s.url.indexOf("http") && s.dataType == "script" ) {
220 var head = document.getElementsByTagName("head")[0];
221 var script = document.createElement("script");
224 // Handle Script loading
225 if ( !jsonp && (s.success || s.complete) ) {
228 // Attach handlers for all browsers
229 script.onload = script.onreadystatechange = function(){
230 if ( !done && (!this.readyState ||
231 this.readyState == "loaded" || this.readyState == "complete") ) {
235 head.removeChild( script );
240 head.appendChild(script);
242 // We handle everything using the script element injection
246 var requestDone = false;
248 // Create the request object; Microsoft failed to properly
249 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
250 var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
253 xml.open(s.type, s.url, s.async);
255 // Set the correct header, if data is being sent
257 xml.setRequestHeader("Content-Type", s.contentType);
259 // Set the If-Modified-Since header, if ifModified mode.
261 xml.setRequestHeader("If-Modified-Since",
262 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
264 // Set header so the called script knows that it's an XMLHttpRequest
265 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
267 // Allow custom headers/mimetypes
272 jQuery.event.trigger("ajaxSend", [xml, s]);
274 // Wait for a response to come back
275 var onreadystatechange = function(isTimeout){
276 // The transfer is complete and the data is available, or the request timed out
277 if ( !requestDone && xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
280 // clear poll interval
286 status = isTimeout == "timeout" && "timeout" ||
287 !jQuery.httpSuccess( xml ) && "error" ||
288 s.ifModified && jQuery.httpNotModified( xml, s.url ) && "notmodified" ||
291 if ( status == "success" ) {
292 // Watch for, and catch, XML document parse errors
294 // process the data (runs the xml through httpData regardless of callback)
295 data = jQuery.httpData( xml, s.dataType );
297 status = "parsererror";
301 // Make sure that the request was successful or notmodified
302 if ( status == "success" ) {
303 // Cache Last-Modified header, if ifModified mode.
306 modRes = xml.getResponseHeader("Last-Modified");
307 } catch(e) {} // swallow exception thrown by FF if header is not available
309 if ( s.ifModified && modRes )
310 jQuery.lastModified[s.url] = modRes;
312 // JSONP handles its own success callback
316 jQuery.handleError(s, xml, status);
318 // Fire the complete handlers
328 // don't attach the handler to the request, just poll it instead
329 var ival = setInterval(onreadystatechange, 13);
333 setTimeout(function(){
334 // Check to see if the request is still happening
336 // Cancel the request
340 onreadystatechange( "timeout" );
349 jQuery.handleError(s, xml, null, e);
352 // firefox 1.5 doesn't fire statechange for sync requests
354 onreadystatechange();
356 // return XMLHttpRequest to allow aborting the request etc.
360 // If a local callback was specified, fire it and pass it the data
362 s.success( data, status );
364 // Fire the global callback
366 jQuery.event.trigger( "ajaxSuccess", [xml, s] );
372 s.complete(xml, status);
374 // The request was completed
376 jQuery.event.trigger( "ajaxComplete", [xml, s] );
378 // Handle the global AJAX counter
379 if ( s.global && ! --jQuery.active )
380 jQuery.event.trigger( "ajaxStop" );
384 handleError: function( s, xml, status, e ) {
385 // If a local callback was specified, fire it
386 if ( s.error ) s.error( xml, status, e );
388 // Fire the global callback
390 jQuery.event.trigger( "ajaxError", [xml, s, e] );
393 // Counter for holding the number of active queries
396 // Determines if an XMLHttpRequest was successful or not
397 httpSuccess: function( r ) {
399 return !r.status && location.protocol == "file:" ||
400 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
401 jQuery.browser.safari && r.status == undefined;
406 // Determines if an XMLHttpRequest returns NotModified
407 httpNotModified: function( xml, url ) {
409 var xmlRes = xml.getResponseHeader("Last-Modified");
411 // Firefox always returns 200. check Last-Modified date
412 return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
413 jQuery.browser.safari && xml.status == undefined;
418 httpData: function( r, type ) {
419 var ct = r.getResponseHeader("content-type");
420 var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0;
421 var data = xml ? r.responseXML : r.responseText;
423 if ( xml && data.documentElement.tagName == "parsererror" )
426 // If the type is "script", eval it in global context
427 if ( type == "script" )
428 jQuery.globalEval( data );
430 // Get the JavaScript object, if JSON is used.
431 if ( type == "json" )
432 data = eval("(" + data + ")");
437 // Serialize an array of form elements or a set of
438 // key/values into a query string
439 param: function( a ) {
442 // If an array was passed in, assume that it is an array
444 if ( a.constructor == Array || a.jquery )
445 // Serialize the form elements
446 jQuery.each( a, function(){
447 s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
450 // Otherwise, assume that it's an object of key/value pairs
452 // Serialize the key/values
454 // If the value is an array then the key names need to be repeated
455 if ( a[j] && a[j].constructor == Array )
456 jQuery.each( a[j], function(){
457 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
460 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
462 // Return the resulting serialization
463 return s.join("&").replace(/%20/g, "+");