Added local timeout option for $.ajax + test
[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 A set of key/value pairs that will be sent to the server.
15          * @param Function callback A function to be executed whenever the data is loaded.
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          * @example $("#feeds").load("feeds.html")
26          * @before <div id="feeds"></div>
27          * @result <div id="feeds"><b>45</b> feeds found.</div>
28          *
29          * @example $("#feeds").load("feeds.html",
30          *   {test: true},
31          *   function() { alert("load is done"); }
32          * );
33          * @desc Same as above, but with an additional parameter
34          * and a callback that is executed when the data was loaded.
35          *
36          * @test stop();
37          * $('#first').load("data/name.php", function() {
38          *      ok( $('#first').text() == 'ERROR', 'Check if content was injected into the DOM' );
39          *      start();
40          * });
41          *
42          * @name load
43          * @type jQuery
44          * @param String url The URL of the HTML file to load.
45          * @param Hash params A set of key/value pairs that will be sent to the server.
46          * @param Function callback A function to be executed whenever the data is loaded.
47          * @cat AJAX
48          */
49         load: function( url, params, callback, ifModified ) {
50                 if ( url.constructor == Function )
51                         return this.bind("load", url);
52         
53                 callback = callback || function(){};
54         
55                 // Default to a GET request
56                 var type = "GET";
57         
58                 // If the second parameter was provided
59                 if ( params ) {
60                         // If it's a function
61                         if ( params.constructor == Function ) {
62                                 // We assume that it's the callback
63                                 callback = params;
64                                 params = null;
65                                 
66                         // Otherwise, build a param string
67                         } else {
68                                 params = jQuery.param( params );
69                                 type = "POST";
70                         }
71                 }
72                 
73                 var self = this;
74                 
75                 // Request the remote document
76                 jQuery.ajax( type, url, params,function(res, status){
77                         
78                         if ( status == "success" || !ifModified && status == "notmodified" ) {
79                                 // Inject the HTML into all the matched elements
80                                 self.html(res.responseText).each( callback, [res.responseText, status] );
81                                 
82                                 // Execute all the scripts inside of the newly-injected HTML
83                                 $("script", self).each(function(){
84                                         if ( this.src )
85                                                 $.getScript( this.src );
86                                         else
87                                                 eval.call( window, this.text || this.textContent || this.innerHTML || "" );
88                                 });
89                         } else
90                                 callback.apply( self, [res.responseText, status] );
91         
92                 }, ifModified);
93                 
94                 return this;
95         },
96
97         /**
98          * A function for serializing a set of input elements into
99          * a string of data.
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          * @test var data = $(':input').serialize();
108          * ok( data == 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo[bar]=&name=name&button=&=foobar&select1=&select2=3&select3=1', 'Check form serialization as query string' );
109          *
110          * @name serialize
111          * @type String
112          * @cat AJAX
113          */
114         serialize: function() {
115                 return $.param( this );
116         }
117         
118 });
119
120 // If IE is used, create a wrapper for the XMLHttpRequest object
121 if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
122         XMLHttpRequest = function(){
123                 return new ActiveXObject(
124                         navigator.userAgent.indexOf("MSIE 5") >= 0 ?
125                         "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
126                 );
127         };
128
129 // Attach a bunch of functions for handling common AJAX events
130
131 /**
132  * Attach a function to be executed whenever an AJAX request begins.
133  *
134  * @example $("#loading").ajaxStart(function(){
135  *   $(this).show();
136  * });
137  * @desc Show a loading message whenever an AJAX request starts.
138  *
139  * @name ajaxStart
140  * @type jQuery
141  * @param Function callback The function to execute.
142  * @cat AJAX
143  */
144  
145 /**
146  * Attach a function to be executed whenever all AJAX requests have ended.
147  *
148  * @example $("#loading").ajaxStop(function(){
149  *   $(this).hide();
150  * });
151  * @desc Hide a loading message after all the AJAX requests have stopped.
152  *
153  * @name ajaxStop
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 an AJAX request completes.
161  *
162  * @example $("#msg").ajaxComplete(function(){
163  *   $(this).append("<li>Request Complete.</li>");
164  * });
165  * @desc Show a message when an AJAX request completes.
166  *
167  * @name ajaxComplete
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  * successfully.
176  *
177  * @example $("#msg").ajaxSuccess(function(){
178  *   $(this).append("<li>Successful Request!</li>");
179  * });
180  * @desc Show a message when an AJAX request completes successfully.
181  *
182  * @name ajaxSuccess
183  * @type jQuery
184  * @param Function callback The function to execute.
185  * @cat AJAX
186  */
187  
188 /**
189  * Attach a function to be executed whenever an AJAX request fails.
190  *
191  * @example $("#msg").ajaxError(function(){
192  *   $(this).append("<li>Error requesting page.</li>");
193  * });
194  * @desc Show a message when an AJAX request fails.
195  *
196  * @name ajaxError
197  * @type jQuery
198  * @param Function callback The function to execute.
199  * @cat AJAX
200  */
201  
202 /**
203  * @test stop(); var counter = { complete: 0, success: 0, error: 0 };
204  * var success = function() { counter.success++ };
205  * var error = function() { counter.error++ };
206  * var complete = function() { counter.complete++ };
207  * $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
208  * // start with successful test
209  * $.ajax({url: "data/name.php", success: success, error: error, complete: function() {
210  *   ok( counter.error == 0, 'Check succesful request' );
211  *   ok( counter.success == 2, 'Check succesful request' );
212  *   ok( counter.complete == 3, 'Check succesful request' );
213  *   counter.error = 0; counter.success = 0; counter.complete = 0;
214  *   $.ajaxTimeout(500);
215  *   $.ajax({url: "data/name.php?wait=5", success: success, error: error, complete: function() {
216  *     ok( counter.error == 2, 'Check failed request' );
217  *     ok( counter.success == 0, 'Check failed request' );
218  *     ok( counter.complete == 3, 'Check failed request' );
219  *     counter.error = 0; counter.success = 0; counter.complete = 0;
220  *     $.ajaxTimeout(0);
221  *     $.ajax({url: "data/name.php?wait=5", global: false, success: success, error: error, complete: function() {
222  *       ok( counter.error == 0, 'Check sucesful request without globals' );
223  *       ok( counter.success == 1, 'Check sucesful request without globals' );
224  *       ok( counter.complete == 0, 'Check sucesful request without globals' );
225  *       counter.error = 0; counter.success = 0; counter.complete = 0;
226  *       $.ajaxTimeout(500);
227  *       $.ajax({url: "data/name.php?wait=5", global: false, success: success, error: error, complete: function() {
228  *         ok( counter.error == 1, 'Check failedrequest without globals' );
229  *         ok( counter.success == 0, 'Check failed request without globals' );
230  *         ok( counter.complete == 0, 'Check failed request without globals' );
231  *         start();
232  *       }});
233  *     }});
234  *   }});
235  * }});
236  * 
237  * @name ajaxHandlersTesting
238  * @private
239  */
240  
241
242 new function(){
243         var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(",");
244         
245         for ( var i = 0; i < e.length; i++ ) new function(){
246                 var o = e[i];
247                 jQuery.fn[o] = function(f){
248                         return this.bind(o, f);
249                 };
250         };
251 };
252
253 jQuery.extend({
254
255         /**
256          * Load a remote page using an HTTP GET request. All of the arguments to
257          * the method (except URL) are optional.
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          * @test stop();
275          * $.get("data/dashboard.xml", function(xml) {
276          *     var content = [];
277      *     $('tab', xml).each(function(k) {
278      *         // workaround for IE needed here, $(this).text() throws an error
279      *         // content[k] = $.trim(this.firstChild.data) || $(this).text();
280      *         content[k] = $(this).text();
281      *     });
282          *         ok( content[0] && content[0].match(/blabla/), 'Check first tab' );
283          *     ok( content[1] && content[1].match(/blublu/), 'Check second tab' );
284          *     start();
285          * });
286          *
287          * @name $.get
288          * @type jQuery
289          * @param String url The URL of the page to load.
290          * @param Hash params A set of key/value pairs that will be sent to the server.
291          * @param Function callback A function to be executed whenever the data is loaded.
292          * @cat AJAX
293          */
294         get: function( url, data, callback, type, ifModified ) {
295                 if ( data.constructor == Function ) {
296                         type = callback;
297                         callback = data;
298                         data = null;
299                 }
300                 
301                 if ( data ) url += "?" + jQuery.param(data);
302                 
303                 // Build and start the HTTP Request
304                 jQuery.ajax( "GET", url, null, function(r, status) {
305                         if ( callback ) callback( jQuery.httpData(r,type), status );
306                 }, ifModified);
307         },
308         
309         /**
310          * Load a remote page using an HTTP GET request, only if it hasn't
311          * been modified since it was last retrieved. All of the arguments to
312          * the method (except URL) are optional.
313          *
314          * @example $.getIfModified("test.html")
315          *
316          * @example $.getIfModified("test.html", { name: "John", time: "2pm" } )
317          *
318          * @example $.getIfModified("test.cgi", function(data){
319          *   alert("Data Loaded: " + data);
320          * })
321          *
322          * @example $.getifModified("test.cgi",
323          *   { name: "John", time: "2pm" },
324          *   function(data){
325          *     alert("Data Loaded: " + data);
326          *   }
327          * )
328          *
329          * @test stop();
330          * $.getIfModified("data/name.php", function(msg) {
331          *     ok( msg == 'ERROR', 'Check ifModified' );
332          *     start();
333          * });
334          *
335          * @name $.getIfModified
336          * @type jQuery
337          * @param String url The URL of the page to load.
338          * @param Hash params A set of key/value pairs that will be sent to the server.
339          * @param Function callback A function to be executed whenever the data is loaded.
340          * @cat AJAX
341          */
342         getIfModified: function( url, data, callback, type ) {
343                 jQuery.get(url, data, callback, type, 1);
344         },
345
346         /**
347          * Loads, and executes, a remote JavaScript file using an HTTP GET request.
348          * All of the arguments to the method (except URL) are optional.
349          *
350          * @example $.getScript("test.js")
351          *
352          * @example $.getScript("test.js", function(){
353          *   alert("Script loaded and executed.");
354          * })
355          *
356          * @test stop();
357          * $.getScript("data/test.js", function() {
358          *      ok( foobar == "bar", 'Check if script was evaluated' );
359          *      start();
360          * });
361          *
362          * @name $.getScript
363          * @type jQuery
364          * @param String url The URL of the page to load.
365          * @param Function callback A function to be executed whenever the data is loaded.
366          * @cat AJAX
367          */
368         getScript: function( url, callback ) {
369                 jQuery.get(url, callback, "script");
370         },
371         
372         /**
373          * Load a remote JSON object using an HTTP GET request.
374          * All of the arguments to the method (except URL) are optional.
375          *
376          * @example $.getJSON("test.js", function(json){
377          *   alert("JSON Data: " + json.users[3].name);
378          * })
379          *
380          * @example $.getJSON("test.js",
381          *   { name: "John", time: "2pm" },
382          *   function(json){
383          *     alert("JSON Data: " + json.users[3].name);
384          *   }
385          * )
386          *
387          * @test stop();
388          * $.getJSON("data/json.php", {json: "array"}, function(json) {
389          *   ok( json[0].name == 'John', 'Check JSON: first, name' );
390          *   ok( json[0].age == 21, 'Check JSON: first, age' );
391          *   ok( json[1].name == 'Peter', 'Check JSON: second, name' );
392          *   ok( json[1].age == 25, 'Check JSON: second, age' );
393          *   start();
394          * });
395          * @test stop();
396          * $.getJSON("data/json.php", function(json) {
397          *   ok( json.data.lang == 'en', 'Check JSON: lang' );
398          *   ok( json.data.length == 25, 'Check JSON: length' );
399          *   start();
400          * });
401          *
402          * @name $.getJSON
403          * @type jQuery
404          * @param String url The URL of the page to load.
405          * @param Hash params A set of key/value pairs that will be sent to the server.
406          * @param Function callback A function to be executed whenever the data is loaded.
407          * @cat AJAX
408          */
409         getJSON: function( url, data, callback ) {
410                 if(callback)
411                         jQuery.get(url, data, callback, "json");
412                 else {
413                         jQuery.get(url, data, "json");
414                 }
415         },
416         
417         /**
418          * Load a remote page using an HTTP POST request. All of the arguments to
419          * the method (except URL) are optional.
420          *
421          * @example $.post("test.cgi")
422          *
423          * @example $.post("test.cgi", { name: "John", time: "2pm" } )
424          *
425          * @example $.post("test.cgi", function(data){
426          *   alert("Data Loaded: " + data);
427          * })
428          *
429          * @example $.post("test.cgi",
430          *   { name: "John", time: "2pm" },
431          *   function(data){
432          *     alert("Data Loaded: " + data);
433          *   }
434          * )
435          *
436          * @test stop();
437          * $.post("data/name.php", {xml: "5-2"}, function(xml){
438          *   $('math', xml).each(function() {
439          *          ok( $('calculation', this).text() == '5-2', 'Check for XML' );
440          *          ok( $('result', this).text() == '3', 'Check for XML' );
441          *       });
442          *   start();
443          * });
444          *
445          * @name $.post
446          * @type jQuery
447          * @param String url The URL of the page to load.
448          * @param Hash params A set of key/value pairs that will be sent to the server.
449          * @param Function callback A function to be executed whenever the data is loaded.
450          * @cat AJAX
451          */
452         post: function( url, data, callback, type ) {
453                 // Build and start the HTTP Request
454                 jQuery.ajax( "POST", url, jQuery.param(data), function(r, status) {
455                         if ( callback ) callback( jQuery.httpData(r,type), status );
456                 });
457         },
458         
459         // timeout (ms)
460         timeout: 0,
461
462         /**
463          * Set the timeout of all AJAX requests to a specific amount of time.
464          * This will make all future AJAX requests timeout after a specified amount
465          * of time (the default is no timeout).
466          *
467          * @example $.ajaxTimeout( 5000 );
468          * @desc Make all AJAX requests timeout after 5 seconds.
469          *
470          * @test stop();
471          * var passed = 0;
472          * var timeout;
473          * $.ajaxTimeout(1000);
474          * var pass = function() {
475          *      passed++;
476          *      if(passed == 2) {
477          *              ok( true, 'Check local and global callbacks after timeout' );
478          *              clearTimeout(timeout);
479          *      $('#main').unbind("ajaxError");
480          *              start();
481          *      }
482          * };
483          * var fail = function() {
484          *      ok( false, 'Check for timeout failed' );
485          *      start();
486          * };
487          * timeout = setTimeout(fail, 1500);
488          * $('#main').ajaxError(pass);
489          * $.ajax({
490          *   type: "GET",
491          *   url: "data/name.php?wait=5",
492          *   error: pass,
493          *   success: fail
494          * });
495          *
496          * @test stop(); $.ajaxTimeout(50);
497          * $.ajax({
498          *   type: "GET",
499          *   timeout: 5000,
500          *   url: "data/name.php?wait=1",
501          *   error: function() {
502          *         ok( false, 'Check for local timeout failed' );
503          *         start();
504          *   },
505          *   success: function() {
506          *     ok( true, 'Check for local timeout' );
507          *     start();
508          *   }
509          * });
510          * 
511          *
512          * @name $.ajaxTimeout
513          * @type jQuery
514          * @param Number time How long before an AJAX request times out.
515          * @cat AJAX
516          */
517         ajaxTimeout: function(timeout) {
518                 jQuery.timeout = timeout;
519         },
520
521         // Last-Modified header cache for next request
522         lastModified: {},
523         
524         /**
525          * Load a remote page using an HTTP request. This function is the primary
526          * means of making AJAX requests using jQuery. $.ajax() takes one property,
527          * an object of key/value pairs, that're are used to initalize the request.
528          *
529          * These are all the key/values that can be passed in to 'prop':
530          *
531          * (String) type - The type of request to make (e.g. "POST" or "GET").
532          *
533          * (String) url - The URL of the page to request.
534          * 
535          * (String) data - A string of data to be sent to the server (POST only).
536          *
537          * (String) dataType - The type of data that you're expecting back from
538          * the server (e.g. "xml", "html", "script", or "json").
539          *
540          * (Number) timeout - Local timeout to override global timeout, eg. to give a
541          * single request a longer timeout while all others timeout after 1 seconds,
542          * see $.ajaxTimeout
543          *
544          * (Boolean) global - Wheather to trigger global AJAX event handlers for
545          * this request, default is true. Set to true to prevent that global handlers
546          * like ajaxStart or ajaxStop are triggered.
547          *
548          * (Function) error - A function to be called if the request fails. The
549          * function gets passed two arguments: The XMLHttpRequest object and a
550          * string describing the type of error that occurred.
551          *
552          * (Function) success - A function to be called if the request succeeds. The
553          * function gets passed one argument: The data returned from the server,
554          * formatted according to the 'dataType' parameter.
555          *
556          * (Function) complete - A function to be called when the request finishes. The
557          * function gets passed two arguments: The XMLHttpRequest object and a
558          * string describing the type the success of the request.
559          *
560          * @example $.ajax({
561          *   type: "GET",
562          *   url: "test.js",
563          *   dataType: "script"
564          * })
565          * @desc Load and execute a JavaScript file.
566          *
567          * @example $.ajax({
568          *   type: "POST",
569          *   url: "some.php",
570          *   data: "name=John&location=Boston",
571          *   success: function(msg){
572          *     alert( "Data Saved: " + msg );
573          *   }
574          * });
575          * @desc Save some data to the server and notify the user once its complete.
576          *
577          * @test stop();
578          * $.ajax({
579          *   type: "GET",
580          *   url: "data/name.php?name=foo",
581          *   success: function(msg){
582          *     ok( msg == 'bar', 'Check for GET' );
583          *     start();
584          *   }
585          * });
586          *
587          * @test stop();
588          * $.ajax({
589          *   type: "POST",
590          *   url: "data/name.php",
591          *   data: "name=peter",
592          *   success: function(msg){
593          *     ok( msg == 'pan', 'Check for POST' );
594          *     start();
595          *   }
596          * });
597          *
598          * @name $.ajax
599          * @type jQuery
600          * @param Hash prop A set of properties to initialize the request with.
601          * @cat AJAX
602          */
603         ajax: function( type, url, data, ret, ifModified ) {
604                 // If only a single argument was passed in,
605                 // assume that it is a object of key/value pairs
606                 if ( !url ) {
607                         ret = type.complete;
608                         var success = type.success;
609                         var error = type.error;
610                         var dataType = type.dataType;
611                         var global = typeof type.global == "boolean" ? type.global : true;
612                         var timeout = typeof type.timeout == "number" ? type.timeout : jQuery.timeout;
613                         data = type.data;
614                         url = type.url;
615                         type = type.type;
616                 }
617                 
618                 // Watch for a new set of requests
619                 if ( global && ! jQuery.active++ )
620                         jQuery.event.trigger( "ajaxStart" );
621
622                 var requestDone = false;
623         
624                 // Create the request object
625                 var xml = new XMLHttpRequest();
626         
627                 // Open the socket
628                 xml.open(type || "GET", url, true);
629                 
630                 // Set the correct header, if data is being sent
631                 if ( data )
632                         xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
633                 
634                 // Set the If-Modified-Since header, if ifModified mode.
635                 if ( ifModified )
636                         xml.setRequestHeader("If-Modified-Since",
637                                 jQuery.lastModified[url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
638                 
639                 // Set header so the called script knows that it's an XMLHttpRequest
640                 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
641         
642                 // Make sure the browser sends the right content length
643                 if ( xml.overrideMimeType )
644                         xml.setRequestHeader("Connection", "close");
645                 
646                 // Wait for a response to come back
647                 var onreadystatechange = function(istimeout){
648                         // The transfer is complete and the data is available, or the request timed out
649                         if ( xml && (xml.readyState == 4 || istimeout == "timeout") ) {
650                                 requestDone = true;
651
652                                 var status = jQuery.httpSuccess( xml ) && istimeout != "timeout" ?
653                                         ifModified && jQuery.httpNotModified( xml, url ) ? "notmodified" : "success" : "error";
654                                 
655                                 // Make sure that the request was successful or notmodified
656                                 if ( status != "error" ) {
657                                         // Cache Last-Modified header, if ifModified mode.
658                                         var modRes = xml.getResponseHeader("Last-Modified");
659                                         if ( ifModified && modRes ) jQuery.lastModified[url] = modRes;
660                                         
661                                         // If a local callback was specified, fire it
662                                         if ( success )
663                                                 success( jQuery.httpData( xml, dataType ), status );
664                                         
665                                         // Fire the global callback
666                                         if( global )
667                                                 jQuery.event.trigger( "ajaxSuccess" );
668                                 
669                                 // Otherwise, the request was not successful
670                                 } else {
671                                         // If a local callback was specified, fire it
672                                         if ( error ) error( xml, status );
673                                         
674                                         // Fire the global callback
675                                         if( global )
676                                                 jQuery.event.trigger( "ajaxError" );
677                                 }
678                                 
679                                 // The request was completed
680                                 if( global )
681                                         jQuery.event.trigger( "ajaxComplete" );
682                                 
683                                 // Handle the global AJAX counter
684                                 if ( global && ! --jQuery.active )
685                                         jQuery.event.trigger( "ajaxStop" );
686         
687                                 // Process result
688                                 if ( ret ) ret(xml, status);
689                                 
690                                 // Stop memory leaks
691                                 xml.onreadystatechange = function(){};
692                                 xml = null;
693                                 
694                         }
695                 };
696                 xml.onreadystatechange = onreadystatechange;
697                 
698                 // Timeout checker
699                 if(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 ) onreadystatechange( "timeout" );
707
708                                         // Clear from memory
709                                         xml = null;
710                                 }
711                         }, timeout);
712                 
713                 // Send the data
714                 xml.send(data);
715         },
716         
717         // Counter for holding the number of active queries
718         active: 0,
719         
720         // Determines if an XMLHttpRequest was successful or not
721         httpSuccess: function(r) {
722                 try {
723                         return !r.status && location.protocol == "file:" ||
724                                 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
725                                 jQuery.browser.safari && r.status == undefined;
726                 } catch(e){}
727
728                 return false;
729         },
730
731         // Determines if an XMLHttpRequest returns NotModified
732         httpNotModified: function(xml, url) {
733                 try {
734                         var xmlRes = xml.getResponseHeader("Last-Modified");
735
736                         // Firefox always returns 200. check Last-Modified date
737                         return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
738                                 jQuery.browser.safari && xml.status == undefined;
739                 } catch(e){}
740
741                 return false;
742         },
743         
744         /* Get the data out of an XMLHttpRequest.
745          * Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
746          * otherwise return plain text.
747          * (String) data - The type of data that you're expecting back,
748          * (e.g. "xml", "html", "script")
749          */
750         httpData: function(r,type) {
751                 var ct = r.getResponseHeader("content-type");
752                 var data = !type && ct && ct.indexOf("xml") >= 0;
753                 data = type == "xml" || data ? r.responseXML : r.responseText;
754
755                 // If the type is "script", eval it
756                 if ( type == "script" ) eval.call( window, data );
757
758                 // Get the JavaScript object, if JSON is used.
759                 if ( type == "json" ) eval( "data = " + data );
760
761                 return data;
762         },
763         
764         // Serialize an array of form elements or a set of
765         // key/values into a query string
766         param: function(a) {
767                 var s = [];
768                 
769                 // If an array was passed in, assume that it is an array
770                 // of form elements
771                 if ( a.constructor == Array || a.jquery ) {
772                         // Serialize the form elements
773                         for ( var i = 0; i < a.length; i++ )
774                                 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
775                         
776                 // Otherwise, assume that it's an object of key/value pairs
777                 } else {
778                         // Serialize the key/values
779                         for ( var j in a )
780                                 s.push( j + "=" + encodeURIComponent( a[j] ) );
781                 }
782                 
783                 // Return the resulting serialization
784                 return s.join("&");
785         }
786
787 });