3 // http://jquery.com/docs/ajax/
6 * Load HTML from a remote file and inject it into the DOM
8 jQuery.fn.loadIfModified = function( url, params, callback ) {
9 this.load( url, params, callback, 1 );
12 jQuery.fn.load = function( url, params, callback, ifModified ) {
13 if ( url.constructor == Function )
14 return this.bind("load", url);
16 callback = callback || function(){};
18 // Default to a GET request
21 // If the second parameter was provided
24 if ( params.constructor == Function ) {
25 // We assume that it's the callback
29 // Otherwise, build a param string
31 params = jQuery.param( params );
38 // Request the remote document
39 jQuery.ajax( type, url, params,function(res, status){
41 if ( status == "success" || !ifModified && status == "notmodified" ) {
42 // Inject the HTML into all the matched elements
43 self.html(res.responseText).each( callback, [res.responseText, status] );
45 // Execute all the scripts inside of the newly-injected HTML
46 $("script", self).each(function(){
48 $.getScript( this.src );
50 eval.call( window, this.text || this.textContent || this.innerHTML || "" );
53 callback.apply( self, [res.responseText, status] );
60 // If IE is used, create a wrapper for the XMLHttpRequest object
61 if ( jQuery.browser.msie )
62 XMLHttpRequest = function(){
63 return new ActiveXObject(
64 navigator.userAgent.indexOf("MSIE 5") >= 0 ?
65 "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
69 // Attach a bunch of functions for handling common AJAX events
71 var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(',');
73 for ( var i = 0; i < e.length; i++ ) new function(){
75 jQuery.fn[o] = function(f){
76 return this.bind(o, f);
84 * Load a remote page using a GET request
86 get: function( url, data, callback, type, ifModified ) {
87 if ( data.constructor == Function ) {
93 if ( data ) url += "?" + jQuery.param(data);
95 // Build and start the HTTP Request
96 jQuery.ajax( "GET", url, null, function(r, status) {
97 if ( callback ) callback( jQuery.httpData(r,type), status );
101 getIfModified: function( url, data, callback, type ) {
102 jQuery.get(url, data, callback, type, 1);
105 getScript: function( url, data, callback ) {
106 jQuery.get(url, data, callback, "script");
110 * Load a remote page using a POST request.
112 post: function( url, data, callback, type ) {
113 // Build and start the HTTP Request
114 jQuery.ajax( "POST", url, jQuery.param(data), function(r, status) {
115 if ( callback ) callback( jQuery.httpData(r,type), status );
122 ajaxTimeout: function(timeout) {
123 jQuery.timeout = timeout;
126 // Last-Modified header cache for next request
130 * A common wrapper for making XMLHttpRequests
132 ajax: function( type, url, data, ret, ifModified ) {
133 // If only a single argument was passed in,
134 // assume that it is a object of key/value pairs
137 var success = type.success;
138 var error = type.error;
144 // Watch for a new set of requests
145 if ( ! jQuery.active++ )
146 jQuery.event.trigger( "ajaxStart" );
148 // Create the request object
149 var xml = new XMLHttpRequest();
152 xml.open(type || "GET", url, true);
154 // Set the correct header, if data is being sent
156 xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
158 // Set the If-Modified-Since header, if ifModified mode.
160 xml.setRequestHeader("If-Modified-Since",
161 jQuery.lastModified[url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
163 // Set header so calling script knows that it's an XMLHttpRequest
164 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
166 // Make sure the browser sends the right content length
167 if ( xml.overrideMimeType )
168 xml.setRequestHeader("Connection", "close");
170 // Wait for a response to come back
171 var onreadystatechange = function(istimeout){
172 // The transfer is complete and the data is available, or the request timed out
173 if ( xml && (xml.readyState == 4 || istimeout) ) {
174 var status = jQuery.httpSuccess( xml ) && !istimeout ?
175 ifModified && jQuery.httpNotModified( xml, url ) ? "notmodified" : "success" : "error";
177 // Make sure that the request was successful or notmodified
178 if ( status != "error" ) {
179 // Cache Last-Modified header, if ifModified mode.
180 var modRes = xml.getResponseHeader("Last-Modified");
181 if ( ifModified && modRes ) jQuery.lastModified[url] = modRes;
183 // If a local callback was specified, fire it
184 if ( success ) success( xml, status );
186 // Fire the global callback
187 jQuery.event.trigger( "ajaxSuccess" );
189 // Otherwise, the request was not successful
191 // If a local callback was specified, fire it
192 if ( error ) error( xml, status );
194 // Fire the global callback
195 jQuery.event.trigger( "ajaxError" );
198 // The request was completed
199 jQuery.event.trigger( "ajaxComplete" );
201 // Handle the global AJAX counter
202 if ( ! --jQuery.active )
203 jQuery.event.trigger( "ajaxStop" );
206 if ( ret ) ret(xml, status);
209 xml.onreadystatechange = function(){};
214 xml.onreadystatechange = onreadystatechange;
217 if(jQuery.timeout > 0)
218 setTimeout(function(){
219 // Check to see if the request is still happening
221 // Cancel the request
224 // for Opera. Opera does't call onreadystatechange when aborted.
225 if (xml) onreadystatechange(1);
236 // Counter for holding the number of active queries
239 // Determines if an XMLHttpRequest was successful or not
240 httpSuccess: function(r) {
243 ( r.status >= 200 && r.status < 300 ) || r.status == 304 :
244 location.protocol == "file:";
250 // Determines if an XMLHttpRequest returns NotModified
251 httpNotModified: function(xml, url) {
253 var xmlRes = xml.getResponseHeader("Last-Modified");
255 // Firefox always returns 200. check Last-Modified date
256 if( xml.status == 304 || xmlRes == jQuery.lastModified[url] ) return true;
262 // Get the data out of an XMLHttpRequest.
263 // Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
264 // otherwise return plain text.
265 httpData: function(r,type) {
266 var ct = r.getResponseHeader("content-type");
267 var data = !type && ct && ct.indexOf("xml") >= 0;
268 data = type == "xml" || data ? r.responseXML : r.responseText;
270 // If the type is "script", eval it
271 if ( type == "script" ) eval.call( window, data );
276 // Serialize an array of form elements or a set of
277 // key/values into a query string
281 // If an array was passed in, assume that it is an array
283 if ( a.constructor == Array ) {
284 // Serialize the form elements
285 for ( var i = 0; i < a.length; i++ )
286 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
288 // Otherwise, assume that it's an object of key/value pairs
290 // Serialize the key/values
292 s.push( j + "=" + encodeURIComponent( a[j] ) );
295 // Return the resulting serialization