Documentation for $.ajaxSetup
[jquery.git] / src / ajax / ajax.js
1 jQuery.fn.extend({
2
3         /**
4          * Load HTML from a remote file and inject it into the DOM, only if it's
5          * been modified by the server.
6          *
7          * @example $("#feeds").loadIfModified("feeds.html");
8          * @before <div id="feeds"></div>
9          * @result <div id="feeds"><b>45</b> feeds found.</div>
10          *
11          * @name loadIfModified
12          * @type jQuery
13          * @param String url The URL of the HTML file to load.
14          * @param Hash params (optional) A set of key/value pairs that will be sent to the server.
15          * @param Function callback (optional) A function to be executed whenever the data is loaded (parameters: responseText, status and response itself).
16          * @cat AJAX
17          */
18         loadIfModified: function( url, params, callback ) {
19                 this.load( url, params, callback, 1 );
20         },
21
22         /**
23          * Load HTML from a remote file and inject it into the DOM.
24          *
25          * Note: Avoid to use this to load scripts, instead use $.getScript.
26          *
27          * @example $("#feeds").load("feeds.html");
28          * @before <div id="feeds"></div>
29          * @result <div id="feeds"><b>45</b> feeds found.</div>
30          *
31          * @example $("#feeds").load("feeds.html",
32          *   {limit: 25},
33          *   function() { alert("The last 25 entries in the feed have been loaded"); }
34          * );
35          * @desc Same as above, but with an additional parameter
36          * and a callback that is executed when the data was loaded.
37          *
38          * @name load
39          * @type jQuery
40          * @param String url The URL of the HTML file to load.
41          * @param Object params (optional) A set of key/value pairs that will be sent as data to the server.
42          * @param Function callback (optional) A function to be executed whenever the data is loaded (parameters: responseText, status and response itself).
43          * @cat AJAX
44          */
45         load: function( url, params, callback, ifModified ) {
46                 if ( url.constructor == Function )
47                         return this.bind("load", url);
48
49                 callback = callback || function(){};
50
51                 // Default to a GET request
52                 var type = "GET";
53
54                 // If the second parameter was provided
55                 if ( params ) {
56                         // If it's a function
57                         if ( params.constructor == Function ) {
58                                 // We assume that it's the callback
59                                 callback = params;
60                                 params = null;
61
62                         // Otherwise, build a param string
63                         } else {
64                                 params = jQuery.param( params );
65                                 type = "POST";
66                         }
67                 }
68
69                 var self = this;
70
71                 // Request the remote document
72                 jQuery.ajax({
73                         url: url,
74                         type: type,
75                         data: params,
76                         ifModified: ifModified,
77                         complete: function(res, status){
78                                 if ( status == "success" || !ifModified && status == "notmodified" ) {
79                                         // Inject the HTML into all the matched elements
80                                         self.html(res.responseText)
81                                           // Execute all the scripts inside of the newly-injected HTML
82                                           .evalScripts()
83                                           // Execute callback
84                                           .each( callback, [res.responseText, status, res] );
85                                 } else
86                                         callback.apply( self, [res.responseText, status, res] );
87                         }
88                 });
89                 return this;
90         },
91
92         /**
93          * Serializes a set of input elements into a string of data.
94          * This will serialize all given elements.
95          *
96          * A serialization similar to the form submit of a browser is
97          * provided by the form plugin. It also takes multiple-selects 
98          * into account, while this method recognizes only a single option.
99          *
100          * @example $("input[@type=text]").serialize();
101          * @before <input type='text' name='name' value='John'/>
102          * <input type='text' name='location' value='Boston'/>
103          * @after name=John&location=Boston
104          * @desc Serialize a selection of input elements to a string
105          *
106          * @name serialize
107          * @type String
108          * @cat AJAX
109          */
110         serialize: function() {
111                 return jQuery.param( this );
112         },
113
114         /**
115          * Evaluate all script tags inside this jQuery. If they have a src attribute,
116          * the script is loaded, otherwise it's content is evaluated.
117          *
118          * @name evalScripts
119          * @type jQuery
120          * @private
121          * @cat AJAX
122          */
123         evalScripts: function() {
124                 return this.find('script').each(function(){
125                         if ( this.src )
126                                 jQuery.getScript( this.src );
127                         else {
128                                 jQuery.globalEval( this.text || this.textContent || this.innerHTML || "" );
129                         }
130                 }).end();
131         }
132
133 });
134
135 // If IE is used, create a wrapper for the XMLHttpRequest object
136 if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
137         XMLHttpRequest = function(){
138                 return new ActiveXObject("Microsoft.XMLHTTP");
139         };
140
141 // Attach a bunch of functions for handling common AJAX events
142
143 /**
144  * Attach a function to be executed whenever an AJAX request begins
145  * and there is none already active.
146  *
147  * @example $("#loading").ajaxStart(function(){
148  *   $(this).show();
149  * });
150  * @desc Show a loading message whenever an AJAX request starts
151  * (and none is already active).
152  *
153  * @name ajaxStart
154  * @type jQuery
155  * @param Function callback The function to execute.
156  * @cat AJAX
157  */
158
159 /**
160  * Attach a function to be executed whenever all AJAX requests have ended.
161  *
162  * @example $("#loading").ajaxStop(function(){
163  *   $(this).hide();
164  * });
165  * @desc Hide a loading message after all the AJAX requests have stopped.
166  *
167  * @name ajaxStop
168  * @type jQuery
169  * @param Function callback The function to execute.
170  * @cat AJAX
171  */
172
173 /**
174  * Attach a function to be executed whenever an AJAX request completes.
175  *
176  * The XMLHttpRequest and settings used for that request are passed
177  * as arguments to the callback.
178  *
179  * @example $("#msg").ajaxComplete(function(request, settings){
180  *   $(this).append("<li>Request Complete.</li>");
181  * });
182  * @desc Show a message when an AJAX request completes.
183  *
184  * @name ajaxComplete
185  * @type jQuery
186  * @param Function callback The function to execute.
187  * @cat AJAX
188  */
189
190 /**
191  * Attach a function to be executed whenever an AJAX request completes
192  * successfully.
193  *
194  * The XMLHttpRequest and settings used for that request are passed
195  * as arguments to the callback.
196  *
197  * @example $("#msg").ajaxSuccess(function(request, settings){
198  *   $(this).append("<li>Successful Request!</li>");
199  * });
200  * @desc Show a message when an AJAX request completes successfully.
201  *
202  * @name ajaxSuccess
203  * @type jQuery
204  * @param Function callback The function to execute.
205  * @cat AJAX
206  */
207
208 /**
209  * Attach a function to be executed whenever an AJAX request fails.
210  *
211  * The XMLHttpRequest and settings used for that request are passed
212  * as arguments to the callback. A third argument, an exception object,
213  * is passed if an exception occured while processing the request.
214  *
215  * @example $("#msg").ajaxError(function(request, settings){
216  *   $(this).append("<li>Error requesting page " + settings.url + "</li>");
217  * });
218  * @desc Show a message when an AJAX request fails.
219  *
220  * @name ajaxError
221  * @type jQuery
222  * @param Function callback The function to execute.
223  * @cat AJAX
224  */
225  
226 /**
227  * Attach a function to be executed before an AJAX request is send.
228  *
229  * The XMLHttpRequest and settings used for that request are passed
230  * as arguments to the callback.
231  *
232  * @example $("#msg").ajaxSend(function(request, settings){
233  *   $(this).append("<li>Starting request at " + settings.url + "</li>");
234  * });
235  * @desc Show a message before an AJAX request is send.
236  *
237  * @name ajaxSend
238  * @type jQuery
239  * @param Function callback The function to execute.
240  * @cat AJAX
241  */
242
243 new function(){
244         var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(",");
245
246         for ( var i = 0; i < e.length; i++ ) new function(){
247                 var o = e[i];
248                 jQuery.fn[o] = function(f){
249                         return this.bind(o, f);
250                 };
251         };
252 };
253
254 jQuery.extend({
255
256         /**
257          * Load a remote page using an HTTP GET request.
258          *
259          * @example $.get("test.cgi");
260          *
261          * @example $.get("test.cgi", { name: "John", time: "2pm" } );
262          *
263          * @example $.get("test.cgi", function(data){
264          *   alert("Data Loaded: " + data);
265          * });
266          *
267          * @example $.get("test.cgi",
268          *   { name: "John", time: "2pm" },
269          *   function(data){
270          *     alert("Data Loaded: " + data);
271          *   }
272          * );
273          *
274          * @name $.get
275          * @type XMLHttpRequest
276          * @param String url The URL of the page to load.
277          * @param Hash params (optional) A set of key/value pairs that will be sent to the server.
278          * @param Function callback (optional) A function to be executed whenever the data is loaded.
279          * @cat AJAX
280          */
281         get: function( url, data, callback, type, ifModified ) {
282                 // shift arguments if data argument was ommited
283                 if ( data && data.constructor == Function ) {
284                         callback = data;
285                         data = null;
286                 }
287                 return jQuery.ajax({
288                         url: url,
289                         data: data,
290                         success: callback,
291                         dataType: type,
292                         ifModified: ifModified
293                 });
294         },
295
296         /**
297          * Load a remote page using an HTTP GET request, only if it hasn't
298          * been modified since it was last retrieved.
299          *
300          * @example $.getIfModified("test.html");
301          *
302          * @example $.getIfModified("test.html", { name: "John", time: "2pm" } );
303          *
304          * @example $.getIfModified("test.cgi", function(data){
305          *   alert("Data Loaded: " + data);
306          * });
307          *
308          * @example $.getifModified("test.cgi",
309          *   { name: "John", time: "2pm" },
310          *   function(data){
311          *     alert("Data Loaded: " + data);
312          *   }
313          * );
314          *
315          * @name $.getIfModified
316          * @type XMLHttpRequest
317          * @param String url The URL of the page to load.
318          * @param Hash params (optional) A set of key/value pairs that will be sent to the server.
319          * @param Function callback (optional) A function to be executed whenever the data is loaded.
320          * @cat AJAX
321          */
322         getIfModified: function( url, data, callback, type ) {
323                 return jQuery.get(url, data, callback, type, 1);
324         },
325
326         /**
327          * Loads, and executes, a remote JavaScript file using an HTTP GET request.
328          *
329          * Warning: Safari <= 2.0.x is unable to evalulate scripts in a global
330          * context synchronously. If you load functions via getScript, make sure
331          * to call them after a delay.
332          *
333          * @example $.getScript("test.js");
334          *
335          * @example $.getScript("test.js", function(){
336          *   alert("Script loaded and executed.");
337          * });
338          *
339          * @name $.getScript
340          * @type XMLHttpRequest
341          * @param String url The URL of the page to load.
342          * @param Function callback (optional) A function to be executed whenever the data is loaded.
343          * @cat AJAX
344          */
345         getScript: function( url, callback ) {
346                 return jQuery.get(url, null, callback, "script");
347         },
348
349         /**
350          * Load JSON data using an HTTP GET request.
351          *
352          * @example $.getJSON("test.js", function(json){
353          *   alert("JSON Data: " + json.users[3].name);
354          * });
355          *
356          * @example $.getJSON("test.js",
357          *   { name: "John", time: "2pm" },
358          *   function(json){
359          *     alert("JSON Data: " + json.users[3].name);
360          *   }
361          * );
362          *
363          * @name $.getJSON
364          * @type XMLHttpRequest
365          * @param String url The URL of the page to load.
366          * @param Hash params (optional) A set of key/value pairs that will be sent to the server.
367          * @param Function callback A function to be executed whenever the data is loaded.
368          * @cat AJAX
369          */
370         getJSON: function( url, data, callback ) {
371                 return jQuery.get(url, data, callback, "json");
372         },
373
374         /**
375          * Load a remote page using an HTTP POST request.
376          *
377          * @example $.post("test.cgi");
378          *
379          * @example $.post("test.cgi", { name: "John", time: "2pm" } );
380          *
381          * @example $.post("test.cgi", function(data){
382          *   alert("Data Loaded: " + data);
383          * });
384          *
385          * @example $.post("test.cgi",
386          *   { name: "John", time: "2pm" },
387          *   function(data){
388          *     alert("Data Loaded: " + data);
389          *   }
390          * );
391          *
392          * @name $.post
393          * @type XMLHttpRequest
394          * @param String url The URL of the page to load.
395          * @param Hash params (optional) A set of key/value pairs that will be sent to the server.
396          * @param Function callback (optional) A function to be executed whenever the data is loaded.
397          * @cat AJAX
398          */
399         post: function( url, data, callback, type ) {
400                 return jQuery.ajax({
401                         type: "POST",
402                         url: url,
403                         data: data,
404                         success: callback,
405                         dataType: type
406                 });
407         },
408
409         // timeout (ms)
410         //timeout: 0,
411
412         /**
413          * Set the timeout of all AJAX requests to a specific amount of time.
414          * This will make all future AJAX requests timeout after a specified amount
415          * of time.
416          *
417          * Set to null or 0 to disable timeouts (default).
418          *
419          * You can manually abort requests with the XMLHttpRequest's (returned by
420          * all ajax functions) abort() method.
421          *
422          * Deprecated. Use $.ajaxSetup instead.
423          *
424          * @example $.ajaxTimeout( 5000 );
425          * @desc Make all AJAX requests timeout after 5 seconds.
426          *
427          * @name $.ajaxTimeout
428          * @type undefined
429          * @param Number time How long before an AJAX request times out.
430          * @cat AJAX
431          */
432         ajaxTimeout: function(timeout) {
433                 jQuery.ajaxSettings.timeout = timeout;
434         },
435         
436         /**
437          * Setup global settings for AJAX requests.
438          *
439          * See $.ajax for a description of all available options.
440          *
441          * @example $.ajaxSetup( {
442          *   url: "/xmlhttp/",
443          *   global: false,
444          *   type: "POST"
445          * } );
446          * @desc Sets the defaults for AJAX requests to the url "/xmlhttp/",
447          * disables global handlers and uses POST instead of GET
448          *
449          * @name $.ajaxSetup
450          * @type undefined
451          * @param Object settings Key/value pairs for ajax options
452          * @cat AJAX
453          */
454         ajaxSetup: function(settings) {
455                 jQuery.extend(jQuery.ajaxSettings, settings);
456         },
457
458         ajaxSettings: {
459                 global: true,
460                 type: "GET",
461                 timeout: 0,
462                 contentType: "application/x-www-form-urlencoded",
463                 processData: true,
464                 async: true
465         },
466         
467         // Last-Modified header cache for next request
468         lastModified: {},
469
470         /**
471          * Load a remote page using an HTTP request.
472          *
473          * This is jQuery's low-level AJAX implementation. See $.get, $.post etc. for
474          * higher-level abstractions.
475          *
476          * $.ajax() returns the XMLHttpRequest that it creates. In most cases you won't
477          * need that object to manipulate directly, but it is available if you need to
478          * abort the request manually.
479          *
480          * Note: Make sure the server sends the right mimetype (eg. xml as
481          * "text/xml"). Sending the wrong mimetype will get you into serious
482          * trouble that jQuery can't solve.
483          *
484          * Supported datatypes are (see dataType option):
485          *
486          * "xml": Returns a XML document that can be processed via jQuery.
487          *
488          * "html": Returns HTML as plain text, included script tags are evaluated.
489          *
490          * "script": Evaluates the response as Javascript and returns it as plain text.
491          *
492          * "json": Evaluates the response as JSON and returns a Javascript Object
493          *
494          * $.ajax() takes one argument, an object of key/value pairs, that are
495          * used to initalize and handle the request. These are all the key/values that can
496          * be used:
497          *
498          * (String) url - The URL to request.
499          *
500          * (String) type - The type of request to make ("POST" or "GET"), default is "GET".
501          *
502          * (String) dataType - The type of data that you're expecting back from
503          * the server. No default: If the server sends xml, the responseXML, otherwise
504          * the responseText is passed to the success callback.
505          *
506          * (Boolean) ifModified - Allow the request to be successful only if the
507          * response has changed since the last request. This is done by checking the
508          * Last-Modified header. Default value is false, ignoring the header.
509          *
510          * (Number) timeout - Local timeout to override global timeout, eg. to give a
511          * single request a longer timeout while all others timeout after 1 second.
512          * See $.ajaxTimeout() for global timeouts.
513          *
514          * (Boolean) global - Whether to trigger global AJAX event handlers for
515          * this request, default is true. Set to false to prevent that global handlers
516          * like ajaxStart or ajaxStop are triggered.
517          *
518          * (Function) error - A function to be called if the request fails. The
519          * function gets passed tree arguments: The XMLHttpRequest object, a
520          * string describing the type of error that occurred and an optional
521          * exception object, if one occured.
522          *
523          * (Function) success - A function to be called if the request succeeds. The
524          * function gets passed one argument: The data returned from the server,
525          * formatted according to the 'dataType' parameter.
526          *
527          * (Function) complete - A function to be called when the request finishes. The
528          * function gets passed two arguments: The XMLHttpRequest object and a
529          * string describing the type of success of the request.
530          *
531          * (Object|String) data - Data to be sent to the server. Converted to a query
532          * string, if not already a string. Is appended to the url for GET-requests.
533          * See processData option to prevent this automatic processing.
534          *
535          * (String) contentType - When sending data to the server, use this content-type.
536          * Default is "application/x-www-form-urlencoded", which is fine for most cases.
537          *
538          * (Boolean) processData - By default, data passed in to the data option as an object
539          * other as string will be processed and transformed into a query string, fitting to
540          * the default content-type "application/x-www-form-urlencoded". If you want to send
541          * DOMDocuments, set this option to false.
542          *
543          * (Boolean) async - By default, all requests are send asynchronous (set to true).
544          * If you need synchronous requests, set this option to false.
545          *
546          * (Function) beforeSend - A pre-callback to set custom headers etc., the
547          * XMLHttpRequest is passed as the only argument.
548          *
549          * @example $.ajax({
550          *   type: "GET",
551          *   url: "test.js",
552          *   dataType: "script"
553          * })
554          * @desc Load and execute a JavaScript file.
555          *
556          * @example $.ajax({
557          *   type: "POST",
558          *   url: "some.php",
559          *   data: "name=John&location=Boston",
560          *   success: function(msg){
561          *     alert( "Data Saved: " + msg );
562          *   }
563          * });
564          * @desc Save some data to the server and notify the user once its complete.
565          *
566          * @example var html = $.ajax({
567          *  url: "some.php",
568          *  async: false
569          * }).responseText;
570          * @desc Loads data synchronously. Blocks the browser while the requests is active.
571          * It is better to block user interaction with others means when synchronization is
572          * necessary, instead to block the complete browser.
573          *
574          * @example var xmlDocument = [create xml document];
575          * $.ajax({
576          *   url: "page.php",
577          *   processData: false,
578          *   data: xmlDocument,
579          *   success: handleResponse
580          * });
581          * @desc Sends an xml document as data to the server. By setting the processData
582          * option to false, the automatic conversion of data to strings is prevented.
583          * 
584          * @name $.ajax
585          * @type XMLHttpRequest
586          * @param Hash prop A set of properties to initialize the request with.
587          * @cat AJAX
588          */
589         ajax: function( s ) {
590                 // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
591                 s = jQuery.extend({}, jQuery.ajaxSettings, s);
592
593                 // if data available
594                 if ( s.data ) {
595                         // convert data if not already a string
596                         if (s.processData && typeof s.data != 'string')
597                         s.data = jQuery.param(s.data);
598                         // append data to url for get requests
599                         if( s.type.toLowerCase() == "get" )
600                                 // "?" + data or "&" + data (in case there are already params)
601                                 s.url += ((s.url.indexOf("?") > -1) ? "&" : "?") + s.data;
602                 }
603
604                 // Watch for a new set of requests
605                 if ( s.global && ! jQuery.active++ )
606                         jQuery.event.trigger( "ajaxStart" );
607
608                 var requestDone = false;
609
610                 // Create the request object
611                 var xml = new XMLHttpRequest();
612
613                 // Open the socket
614                 xml.open(s.type, s.url, s.async);
615
616                 // Set the correct header, if data is being sent
617                 if ( s.data )
618                         xml.setRequestHeader("Content-Type", s.contentType);
619
620                 // Set the If-Modified-Since header, if ifModified mode.
621                 if ( s.ifModified )
622                         xml.setRequestHeader("If-Modified-Since",
623                                 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
624
625                 // Set header so the called script knows that it's an XMLHttpRequest
626                 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
627
628                 // Make sure the browser sends the right content length
629                 if ( xml.overrideMimeType )
630                         xml.setRequestHeader("Connection", "close");
631                         
632                 // Allow custom headers/mimetypes
633                 if( s.beforeSend )
634                         s.beforeSend(xml);
635                 if (s.global)
636                     jQuery.event.trigger("ajaxSend", [xml, s]);
637
638                 // Wait for a response to come back
639                 var onreadystatechange = function(isTimeout){
640                         // The transfer is complete and the data is available, or the request timed out
641                         if ( xml && (xml.readyState == 4 || isTimeout == "timeout") ) {
642                                 requestDone = true;
643                                 var status;
644                                 try {
645                                         status = jQuery.httpSuccess( xml ) && isTimeout != "timeout" ?
646                                                 s.ifModified && jQuery.httpNotModified( xml, s.url ) ? "notmodified" : "success" : "error";
647                                         // Make sure that the request was successful or notmodified
648                                         if ( status != "error" ) {
649                                                 // Cache Last-Modified header, if ifModified mode.
650                                                 var modRes;
651                                                 try {
652                                                         modRes = xml.getResponseHeader("Last-Modified");
653                                                 } catch(e) {} // swallow exception thrown by FF if header is not available
654         
655                                                 if ( s.ifModified && modRes )
656                                                         jQuery.lastModified[s.url] = modRes;
657         
658                                                 // process the data (runs the xml through httpData regardless of callback)
659                                                 var data = jQuery.httpData( xml, s.dataType );
660         
661                                                 // If a local callback was specified, fire it and pass it the data
662                                                 if ( s.success )
663                                                         s.success( data, status );
664         
665                                                 // Fire the global callback
666                                                 if( s.global )
667                                                         jQuery.event.trigger( "ajaxSuccess", [xml, s] );
668                                         } else {
669                                                 jQuery.handleError(s, xml, status);
670                                         }
671                                 } catch(e) {
672                                         status = "error";
673                                         jQuery.handleError(s, xml, status, e);
674                                 }
675
676                                 // The request was completed
677                                 if( s.global )
678                                         jQuery.event.trigger( "ajaxComplete", [xml, s] );
679
680                                 // Handle the global AJAX counter
681                                 if ( s.global && ! --jQuery.active )
682                                         jQuery.event.trigger( "ajaxStop" );
683
684                                 // Process result
685                                 if ( s.complete ) s.complete(xml, status);
686
687                                 // Stop memory leaks
688                                 xml.onreadystatechange = function(){};
689                                 xml = null;
690                         }
691                 };
692                 xml.onreadystatechange = onreadystatechange;
693
694                 // Timeout checker
695                 if(s.timeout > 0)
696                         setTimeout(function(){
697                                 // Check to see if the request is still happening
698                                 if(xml) {
699                                         // Cancel the request
700                                         xml.abort();
701
702                                         if( !requestDone )
703                                                 onreadystatechange( "timeout" );
704                                 }
705                         }, s.timeout);
706                         
707                 // save non-leaking reference 
708                 var xml2 = xml;
709
710                 // Send the data
711                 try {
712                         xml2.send(s.data);
713                 } catch(e) {
714                         jQuery.handleError(s, xml, null, e);
715                 }
716                 
717                 // return XMLHttpRequest to allow aborting the request etc.
718                 return xml2;
719         },
720
721         handleError: function(s, xml, status, e) {
722                 // If a local callback was specified, fire it
723                 if ( s.error ) s.error( xml, status, e );
724
725                 // Fire the global callback
726                 if( s.global )
727                         jQuery.event.trigger( "ajaxError", [xml, s, e] );
728         },
729
730         // Counter for holding the number of active queries
731         active: 0,
732
733         // Determines if an XMLHttpRequest was successful or not
734         httpSuccess: function(r) {
735                 try {
736                         return !r.status && location.protocol == "file:" ||
737                                 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
738                                 jQuery.browser.safari && r.status == undefined;
739                 } catch(e){}
740                 return false;
741         },
742
743         // Determines if an XMLHttpRequest returns NotModified
744         httpNotModified: function(xml, url) {
745                 try {
746                         var xmlRes = xml.getResponseHeader("Last-Modified");
747
748                         // Firefox always returns 200. check Last-Modified date
749                         return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
750                                 jQuery.browser.safari && xml.status == undefined;
751                 } catch(e){}
752                 return false;
753         },
754
755         /* Get the data out of an XMLHttpRequest.
756          * Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
757          * otherwise return plain text.
758          * (String) data - The type of data that you're expecting back,
759          * (e.g. "xml", "html", "script")
760          */
761         httpData: function(r,type) {
762                 var ct = r.getResponseHeader("content-type");
763                 var data = !type && ct && ct.indexOf("xml") >= 0;
764                 data = type == "xml" || data ? r.responseXML : r.responseText;
765
766                 // If the type is "script", eval it in global context
767                 if ( type == "script" ) {
768                         jQuery.globalEval( data );
769                 }
770
771                 // Get the JavaScript object, if JSON is used.
772                 if ( type == "json" ) eval( "data = " + data );
773
774                 // evaluate scripts within html
775                 if ( type == "html" ) jQuery("<div>").html(data).evalScripts();
776
777                 return data;
778         },
779
780         // Serialize an array of form elements or a set of
781         // key/values into a query string
782         param: function(a) {
783                 var s = [];
784
785                 // If an array was passed in, assume that it is an array
786                 // of form elements
787                 if ( a.constructor == Array || a.jquery ) {
788                         // Serialize the form elements
789                         for ( var i = 0; i < a.length; i++ )
790                                 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
791
792                 // Otherwise, assume that it's an object of key/value pairs
793                 } else {
794                         // Serialize the key/values
795                         for ( var j in a ) {
796                                 // If the value is an array then the key names need to be repeated
797                                 if( a[j].constructor == Array ) {
798                                         for (var k = 0; k < a[j].length; k++) {
799                                                 s.push( j + "=" + encodeURIComponent( a[j][k] ) );
800                                         }
801                                 } else {
802                                         s.push( j + "=" + encodeURIComponent( a[j] ) );
803                                 }
804                         }
805                 }
806
807                 // Return the resulting serialization
808                 return s.join("&");
809         },
810         
811         // evalulates a script in global context
812         // not reliable for safari
813         globalEval: function(data) {
814                 if (window.execScript)
815                         window.execScript( data );
816                 else if(jQuery.browser.safari)
817                         // safari doesn't provide a synchronous global eval
818                         window.setTimeout( data, 0 );
819                 else
820                         eval.call( window, data );
821         }
822
823 });