3 loadIfModified: function( url, params, callback ) {
4 this.load( url, params, callback, 1 );
7 load: function( url, params, callback, ifModified ) {
8 if ( jQuery.isFunction( url ) )
9 return this.bind("load", url);
11 var off = url.indexOf(" ");
13 var selector = url.slice(off, url.length);
14 url = url.slice(0, off);
17 callback = callback || function(){};
19 // Default to a GET request
22 // If the second parameter was provided
25 if ( jQuery.isFunction( params ) ) {
26 // We assume that it's the callback
30 // Otherwise, build a param string
32 params = jQuery.param( params );
38 // Request the remote document
43 ifModified: ifModified,
44 complete: function(res, status){
45 // If successful, inject the HTML into all the matched elements
46 if ( status == "success" || !ifModified && status == "notmodified" )
47 // See if a selector was specified
49 // Create a dummy div to hold the results
51 // inject the contents of the document in, removing the scripts
52 // to avoid any 'Permission Denied' errors in IE
53 .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))
55 // Locate the specified elements
58 // If not, just inject the full result
61 // Add delay to account for Safari's delay in globalEval
62 setTimeout(function(){
63 self.each( callback, [res.responseText, status, res] );
70 serialize: function() {
71 return jQuery.param( this );
75 // This method no longer does anything - all script evaluation is
76 // taken care of within the HTML injection methods.
77 evalScripts: function(){}
81 // Attach a bunch of functions for handling common AJAX events
82 jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){
83 jQuery.fn[o] = function(f){
84 return this.bind(o, f);
88 var jsc = (new Date).getTime();
91 get: function( url, data, callback, type, ifModified ) {
92 // shift arguments if data argument was ommited
93 if ( jQuery.isFunction( data ) ) {
104 ifModified: ifModified
109 getIfModified: function( url, data, callback, type ) {
110 return jQuery.get(url, data, callback, type, 1);
113 getScript: function( url, callback ) {
114 return jQuery.get(url, null, callback, "script");
117 getJSON: function( url, data, callback ) {
118 return jQuery.get(url, data, callback, "json");
121 post: function( url, data, callback, type ) {
122 if ( jQuery.isFunction( data ) ) {
137 ajaxTimeout: function( timeout ) {
138 jQuery.ajaxSettings.timeout = timeout;
141 ajaxSetup: function( settings ) {
142 jQuery.extend( jQuery.ajaxSettings, settings );
149 contentType: "application/x-www-form-urlencoded",
155 // Last-Modified header cache for next request
158 ajax: function( s ) {
159 var jsonp, jsre = /=(\?|%3F)/g, status, data;
161 // Extend the settings, but re-extend 's' so that it can be
162 // checked again later (in the test suite, specifically)
163 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
165 // convert data if not already a string
166 if ( s.data && s.processData && typeof s.data != "string" )
167 s.data = jQuery.param(s.data);
169 // Break the data into one single string
170 var q = s.url.indexOf("?");
172 s.data = (s.data ? s.data + "&" : "") + s.url.slice(q + 1);
173 s.url = s.url.slice(0, q);
176 // Handle JSONP Parameter Callbacks
177 if ( s.dataType == "jsonp" ) {
178 if ( !s.data || !s.data.match(jsre) )
179 s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
183 // Build temporary JSONP function
184 if ( s.dataType == "json" && s.data && s.data.match(jsre) ) {
185 jsonp = "jsonp" + jsc++;
186 s.data = s.data.replace(jsre, "=" + jsonp);
188 // We need to make sure
189 // that a JSONP style response is executed properly
190 s.dataType = "script";
192 // Handle JSONP-style loading
193 window[ jsonp ] = function(tmp){
197 window[ jsonp ] = undefined;
198 try{ delete window[ jsonp ]; } catch(e){}
202 if ( s.dataType == "script" && s.cache == null )
205 if ( s.cache === false && s.type.toLowerCase() == "get" )
206 s.data = (s.data ? s.data + "&" : "") + "_=" + (new Date()).getTime();
208 // If data is available, append data to url for get requests
209 if ( s.data && s.type.toLowerCase() == "get" ) {
210 s.url += "?" + s.data;
212 // IE likes to send both get and post data, prevent this
216 // Watch for a new set of requests
217 if ( s.global && ! jQuery.active++ )
218 jQuery.event.trigger( "ajaxStart" );
220 // If we're requesting a remote document
221 // and trying to load JSON or Script
222 if ( !s.url.indexOf("http") && s.dataType == "script" ) {
223 var script = document.createElement("script");
226 // Handle Script loading
227 if ( !jsonp && (s.success || s.complete) ) {
230 // Attach handlers for all browsers
231 script.onload = script.onreadystatechange = function(){
232 if ( !done && (!this.readyState ||
233 this.readyState == "loaded" || this.readyState == "complete") ) {
237 document.body.removeChild( script );
242 document.body.appendChild(script);
244 // We handle everything using the script element injection
248 var requestDone = false;
250 // Create the request object; Microsoft failed to properly
251 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
252 var xml = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
255 xml.open(s.type, s.url, s.async);
257 // Set the correct header, if data is being sent
259 xml.setRequestHeader("Content-Type", s.contentType);
261 // Set the If-Modified-Since header, if ifModified mode.
263 xml.setRequestHeader("If-Modified-Since",
264 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
266 // Set header so the called script knows that it's an XMLHttpRequest
267 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
269 // Allow custom headers/mimetypes
274 jQuery.event.trigger("ajaxSend", [xml, s]);
276 // Wait for a response to come back
277 var onreadystatechange = function(isTimeout){
278 // The transfer is complete and the data is available, or the request timed out
279 if ( !requestDone && xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
282 // clear poll interval
288 status = isTimeout == "timeout" && "timeout" ||
289 !jQuery.httpSuccess( xml ) && "error" ||
290 s.ifModified && jQuery.httpNotModified( xml, s.url ) && "notmodified" ||
293 if ( status == "success" ) {
294 // Watch for, and catch, XML document parse errors
296 // process the data (runs the xml through httpData regardless of callback)
297 data = jQuery.httpData( xml, s.dataType );
299 status = "parsererror";
303 // Make sure that the request was successful or notmodified
304 if ( status == "success" ) {
305 // Cache Last-Modified header, if ifModified mode.
308 modRes = xml.getResponseHeader("Last-Modified");
309 } catch(e) {} // swallow exception thrown by FF if header is not available
311 if ( s.ifModified && modRes )
312 jQuery.lastModified[s.url] = modRes;
314 // JSONP handles its own success callback
318 jQuery.handleError(s, xml, status);
320 // Fire the complete handlers
330 // don't attach the handler to the request, just poll it instead
331 var ival = setInterval(onreadystatechange, 13);
335 setTimeout(function(){
336 // Check to see if the request is still happening
338 // Cancel the request
342 onreadystatechange( "timeout" );
351 jQuery.handleError(s, xml, null, e);
354 // firefox 1.5 doesn't fire statechange for sync requests
356 onreadystatechange();
358 // return XMLHttpRequest to allow aborting the request etc.
362 // If a local callback was specified, fire it and pass it the data
364 s.success( data, status );
366 // Fire the global callback
368 jQuery.event.trigger( "ajaxSuccess", [xml, s] );
374 s.complete(xml, status);
376 // The request was completed
378 jQuery.event.trigger( "ajaxComplete", [xml, s] );
380 // Handle the global AJAX counter
381 if ( s.global && ! --jQuery.active )
382 jQuery.event.trigger( "ajaxStop" );
386 handleError: function( s, xml, status, e ) {
387 // If a local callback was specified, fire it
388 if ( s.error ) s.error( xml, status, e );
390 // Fire the global callback
392 jQuery.event.trigger( "ajaxError", [xml, s, e] );
395 // Counter for holding the number of active queries
398 // Determines if an XMLHttpRequest was successful or not
399 httpSuccess: function( r ) {
401 return !r.status && location.protocol == "file:" ||
402 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
403 jQuery.browser.safari && r.status == undefined;
408 // Determines if an XMLHttpRequest returns NotModified
409 httpNotModified: function( xml, url ) {
411 var xmlRes = xml.getResponseHeader("Last-Modified");
413 // Firefox always returns 200. check Last-Modified date
414 return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
415 jQuery.browser.safari && xml.status == undefined;
420 httpData: function( r, type ) {
421 var ct = r.getResponseHeader("content-type");
422 var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0;
423 var data = xml ? r.responseXML : r.responseText;
425 if ( xml && data.documentElement.tagName == "parsererror" )
428 // If the type is "script", eval it in global context
429 if ( type == "script" )
430 jQuery.globalEval( data );
432 // Get the JavaScript object, if JSON is used.
433 if ( type == "json" )
434 data = eval("(" + data + ")");
439 // Serialize an array of form elements or a set of
440 // key/values into a query string
441 param: function( a ) {
444 // If an array was passed in, assume that it is an array
446 if ( a.constructor == Array || a.jquery )
447 // Serialize the form elements
448 jQuery.each( a, function(){
449 s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
452 // Otherwise, assume that it's an object of key/value pairs
454 // Serialize the key/values
456 // If the value is an array then the key names need to be repeated
457 if ( a[j] && a[j].constructor == Array )
458 jQuery.each( a[j], function(){
459 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
462 s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
464 // Return the resulting serialization