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