3 // http://jquery.com/docs/ajax/
6 * Load HTML from a remote file and inject it into the DOM, only if it's
7 * been modified by the server.
9 * @example $("#feeds").loadIfModified("feeds.html")
10 * @before <div id="feeds"></div>
11 * @result <div id="feeds"><b>45</b> feeds found.</div>
13 * @name loadIfModified
15 * @param String url The URL of the HTML file to load.
16 * @param Hash params A set of key/value pairs that will be sent to the server.
17 * @param Function callback A function to be executed whenever the data is loaded.
20 jQuery.fn.loadIfModified = function( url, params, callback ) {
21 this.load( url, params, callback, 1 );
25 * Load HTML from a remote file and inject it into the DOM.
27 * @example $("#feeds").load("feeds.html")
28 * @before <div id="feeds"></div>
29 * @result <div id="feeds"><b>45</b> feeds found.</div>
33 * @param String url The URL of the HTML file to load.
34 * @param Hash params A set of key/value pairs that will be sent to the server.
35 * @param Function callback A function to be executed whenever the data is loaded.
38 jQuery.fn.load = function( url, params, callback, ifModified ) {
39 if ( url.constructor == Function )
40 return this.bind("load", url);
42 callback = callback || function(){};
44 // Default to a GET request
47 // If the second parameter was provided
50 if ( params.constructor == Function ) {
51 // We assume that it's the callback
55 // Otherwise, build a param string
57 params = jQuery.param( params );
64 // Request the remote document
65 jQuery.ajax( type, url, params,function(res, status){
67 if ( status == "success" || !ifModified && status == "notmodified" ) {
68 // Inject the HTML into all the matched elements
69 self.html(res.responseText).each( callback, [res.responseText, status] );
71 // Execute all the scripts inside of the newly-injected HTML
72 $("script", self).each(function(){
74 $.getScript( this.src );
76 eval.call( window, this.text || this.textContent || this.innerHTML || "" );
79 callback.apply( self, [res.responseText, status] );
86 // If IE is used, create a wrapper for the XMLHttpRequest object
87 if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
88 XMLHttpRequest = function(){
89 return new ActiveXObject(
90 navigator.userAgent.indexOf("MSIE 5") >= 0 ?
91 "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
95 // Attach a bunch of functions for handling common AJAX events
97 var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(',');
99 for ( var i = 0; i < e.length; i++ ) new function(){
101 jQuery.fn[o] = function(f){
102 return this.bind(o, f);
110 * Load a remote page using an HTTP GET request.
112 * @example $.get("test.cgi")
116 * @param String url The URL of the HTML file to load.
121 * Load a remote page using an HTTP GET request.
123 * @example $.get("test.cgi", { name: "John", time: "2pm" } )
127 * @param String url The URL of the HTML file to load.
128 * @param Hash params A set of key/value pairs that will be sent to the server.
133 * Load a remote page using an HTTP GET request.
135 * @example $.get("test.cgi", function(){
136 * alert("Data Loaded.");
141 * @param String url The URL of the HTML file to load.
142 * @param Function callback A function to be executed whenever the data is loaded.
147 * Load a remote page using an HTTP GET request.
149 * @example $.get("test.cgi",
150 * { name: "John", time: "2pm" },
151 * function(){ alert("Data Loaded."); }
156 * @param String url The URL of the HTML file to load.
157 * @param Hash params A set of key/value pairs that will be sent to the server.
158 * @param Function callback A function to be executed whenever the data is loaded.
161 get: function( url, data, callback, type, ifModified ) {
162 if ( data.constructor == Function ) {
168 if ( data ) url += "?" + jQuery.param(data);
170 // Build and start the HTTP Request
171 jQuery.ajax( "GET", url, null, function(r, status) {
172 if ( callback ) callback( jQuery.httpData(r,type), status );
177 * Load a remote page using an HTTP GET request, and only if it hasn't
178 * been modified since it was last retieved.
180 * @example $.getIfModified("test.cgi")
182 * @name $.getIfModified
184 * @param String url The URL of the HTML file to load.
189 * Load a remote page using an HTTP GET request, and only if it hasn't
190 * been modified since it was last retieved.
192 * @example $.getIfModified("test.cgi", { name: "John", time: "2pm" })
194 * @name $.getIfModified
196 * @param String url The URL of the HTML file to load.
197 * @param Hash params A set of key/value pairs that will be sent to the server.
202 * Load a remote page using an HTTP GET request, and only if it hasn't
203 * been modified since it was last retieved.
205 * @example $.getIfModified("test.cgi", function(){
206 * alert("Data Loaded.");
209 * @name $.getIfModified
211 * @param String url The URL of the HTML file to load.
212 * @param Function callback A function to be executed whenever the data is loaded.
217 * Load a remote page using an HTTP GET request, and only if it hasn't
218 * been modified since it was last retieved.
220 * @example $.getIfModified("test.cgi",
221 * { name: "John", time: "2pm" },
222 * function(){ alert("Data Loaded."); }
225 * @name $.getIfModified
227 * @param String url The URL of the HTML file to load.
228 * @param Hash params A set of key/value pairs that will be sent to the server.
229 * @param Function callback A function to be executed whenever the data is loaded.
232 getIfModified: function( url, data, callback, type ) {
233 jQuery.get(url, data, callback, type, 1);
236 getScript: function( url, data, callback ) {
237 jQuery.get(url, data, callback, "script");
241 * Load a remote page using a POST request.
243 post: function( url, data, callback, type ) {
244 // Build and start the HTTP Request
245 jQuery.ajax( "POST", url, jQuery.param(data), function(r, status) {
246 if ( callback ) callback( jQuery.httpData(r,type), status );
253 ajaxTimeout: function(timeout) {
254 jQuery.timeout = timeout;
257 // Last-Modified header cache for next request
261 * A common wrapper for making XMLHttpRequests
263 ajax: function( type, url, data, ret, ifModified ) {
264 // If only a single argument was passed in,
265 // assume that it is a object of key/value pairs
268 var success = type.success;
269 var error = type.error;
275 // Watch for a new set of requests
276 if ( ! jQuery.active++ )
277 jQuery.event.trigger( "ajaxStart" );
279 var requestDone = false;
281 // Create the request object
282 var xml = new XMLHttpRequest();
285 xml.open(type || "GET", url, true);
287 // Set the correct header, if data is being sent
289 xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
291 // Set the If-Modified-Since header, if ifModified mode.
293 xml.setRequestHeader("If-Modified-Since",
294 jQuery.lastModified[url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
296 // Set header so calling script knows that it's an XMLHttpRequest
297 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
299 // Make sure the browser sends the right content length
300 if ( xml.overrideMimeType )
301 xml.setRequestHeader("Connection", "close");
303 // Wait for a response to come back
304 var onreadystatechange = function(istimeout){
305 // The transfer is complete and the data is available, or the request timed out
306 if ( xml && (xml.readyState == 4 || istimeout == "timeout") ) {
309 var status = jQuery.httpSuccess( xml ) && istimeout != "timeout" ?
310 ifModified && jQuery.httpNotModified( xml, url ) ? "notmodified" : "success" : "error";
312 // Make sure that the request was successful or notmodified
313 if ( status != "error" ) {
314 // Cache Last-Modified header, if ifModified mode.
315 var modRes = xml.getResponseHeader("Last-Modified");
316 if ( ifModified && modRes ) jQuery.lastModified[url] = modRes;
318 // If a local callback was specified, fire it
319 if ( success ) success( xml, status );
321 // Fire the global callback
322 jQuery.event.trigger( "ajaxSuccess" );
324 // Otherwise, the request was not successful
326 // If a local callback was specified, fire it
327 if ( error ) error( xml, status );
329 // Fire the global callback
330 jQuery.event.trigger( "ajaxError" );
333 // The request was completed
334 jQuery.event.trigger( "ajaxComplete" );
336 // Handle the global AJAX counter
337 if ( ! --jQuery.active )
338 jQuery.event.trigger( "ajaxStop" );
341 if ( ret ) ret(xml, status);
344 xml.onreadystatechange = function(){};
349 xml.onreadystatechange = onreadystatechange;
352 if(jQuery.timeout > 0)
353 setTimeout(function(){
354 // Check to see if the request is still happening
356 // Cancel the request
359 if ( !requestDone ) onreadystatechange( "timeout" );
370 // Counter for holding the number of active queries
373 // Determines if an XMLHttpRequest was successful or not
374 httpSuccess: function(r) {
376 return !r.status && location.protocol == "file:" ||
377 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
378 jQuery.browser.safari && r.status == undefined;
384 // Determines if an XMLHttpRequest returns NotModified
385 httpNotModified: function(xml, url) {
387 var xmlRes = xml.getResponseHeader("Last-Modified");
389 // Firefox always returns 200. check Last-Modified date
390 return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
391 jQuery.browser.safari && xml.status == undefined;
397 // Get the data out of an XMLHttpRequest.
398 // Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
399 // otherwise return plain text.
400 httpData: function(r,type) {
401 var ct = r.getResponseHeader("content-type");
402 var data = !type && ct && ct.indexOf("xml") >= 0;
403 data = type == "xml" || data ? r.responseXML : r.responseText;
405 // If the type is "script", eval it
406 if ( type == "script" ) eval.call( window, data );
408 // Get the JavaScript object, if JSON is used.
409 if ( type == "json" ) eval( "data = " + data );
414 // Serialize an array of form elements or a set of
415 // key/values into a query string
419 // If an array was passed in, assume that it is an array
421 if ( a.constructor == Array ) {
422 // Serialize the form elements
423 for ( var i = 0; i < a.length; i++ )
424 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
426 // Otherwise, assume that it's an object of key/value pairs
428 // Serialize the key/values
430 s.push( j + "=" + encodeURIComponent( a[j] ) );
433 // Return the resulting serialization