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