Fixed bug #215
[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                 // append ? + data or & + data, in case there are already params
302                 if ( data ) url += ((url.indexOf("?") > -1) ? "&" : "?") + jQuery.param(data);
303                 
304                 // Build and start the HTTP Request
305                 jQuery.ajax( "GET", url, null, function(r, status) {
306                         if ( callback ) callback( jQuery.httpData(r,type), status );
307                 }, ifModified);
308         },
309         
310         /**
311          * Load a remote page using an HTTP GET request, only if it hasn't
312          * been modified since it was last retrieved. All of the arguments to
313          * the method (except URL) are optional.
314          *
315          * @example $.getIfModified("test.html")
316          *
317          * @example $.getIfModified("test.html", { name: "John", time: "2pm" } )
318          *
319          * @example $.getIfModified("test.cgi", function(data){
320          *   alert("Data Loaded: " + data);
321          * })
322          *
323          * @example $.getifModified("test.cgi",
324          *   { name: "John", time: "2pm" },
325          *   function(data){
326          *     alert("Data Loaded: " + data);
327          *   }
328          * )
329          *
330          * @test stop();
331          * $.getIfModified("data/name.php", function(msg) {
332          *     ok( msg == 'ERROR', 'Check ifModified' );
333          *     start();
334          * });
335          *
336          * @name $.getIfModified
337          * @type jQuery
338          * @param String url The URL of the page to load.
339          * @param Hash params A set of key/value pairs that will be sent to the server.
340          * @param Function callback A function to be executed whenever the data is loaded.
341          * @cat AJAX
342          */
343         getIfModified: function( url, data, callback, type ) {
344                 jQuery.get(url, data, callback, type, 1);
345         },
346
347         /**
348          * Loads, and executes, a remote JavaScript file using an HTTP GET request.
349          * All of the arguments to the method (except URL) are optional.
350          *
351          * @example $.getScript("test.js")
352          *
353          * @example $.getScript("test.js", function(){
354          *   alert("Script loaded and executed.");
355          * })
356          *
357          * @test stop();
358          * $.getScript("data/test.js", function() {
359          *      ok( foobar == "bar", 'Check if script was evaluated' );
360          *      start();
361          * });
362          *
363          * @name $.getScript
364          * @type jQuery
365          * @param String url The URL of the page to load.
366          * @param Function callback A function to be executed whenever the data is loaded.
367          * @cat AJAX
368          */
369         getScript: function( url, callback ) {
370                 jQuery.get(url, callback, "script");
371         },
372         
373         /**
374          * Load a remote JSON object using an HTTP GET request.
375          * All of the arguments to the method (except URL) are optional.
376          *
377          * @example $.getJSON("test.js", function(json){
378          *   alert("JSON Data: " + json.users[3].name);
379          * })
380          *
381          * @example $.getJSON("test.js",
382          *   { name: "John", time: "2pm" },
383          *   function(json){
384          *     alert("JSON Data: " + json.users[3].name);
385          *   }
386          * )
387          *
388          * @test stop();
389          * $.getJSON("data/json.php", {json: "array"}, function(json) {
390          *   ok( json[0].name == 'John', 'Check JSON: first, name' );
391          *   ok( json[0].age == 21, 'Check JSON: first, age' );
392          *   ok( json[1].name == 'Peter', 'Check JSON: second, name' );
393          *   ok( json[1].age == 25, 'Check JSON: second, age' );
394          *   start();
395          * });
396          * @test stop();
397          * $.getJSON("data/json.php", function(json) {
398          *   ok( json.data.lang == 'en', 'Check JSON: lang' );
399          *   ok( json.data.length == 25, 'Check JSON: length' );
400          *   start();
401          * });
402          *
403          * @name $.getJSON
404          * @type jQuery
405          * @param String url The URL of the page to load.
406          * @param Hash params A set of key/value pairs that will be sent to the server.
407          * @param Function callback A function to be executed whenever the data is loaded.
408          * @cat AJAX
409          */
410         getJSON: function( url, data, callback ) {
411                 if(callback)
412                         jQuery.get(url, data, callback, "json");
413                 else {
414                         jQuery.get(url, data, "json");
415                 }
416         },
417         
418         /**
419          * Load a remote page using an HTTP POST request. All of the arguments to
420          * the method (except URL) are optional.
421          *
422          * @example $.post("test.cgi")
423          *
424          * @example $.post("test.cgi", { name: "John", time: "2pm" } )
425          *
426          * @example $.post("test.cgi", function(data){
427          *   alert("Data Loaded: " + data);
428          * })
429          *
430          * @example $.post("test.cgi",
431          *   { name: "John", time: "2pm" },
432          *   function(data){
433          *     alert("Data Loaded: " + data);
434          *   }
435          * )
436          *
437          * @test stop();
438          * $.post("data/name.php", {xml: "5-2"}, function(xml){
439          *   $('math', xml).each(function() {
440          *          ok( $('calculation', this).text() == '5-2', 'Check for XML' );
441          *          ok( $('result', this).text() == '3', 'Check for XML' );
442          *       });
443          *   start();
444          * });
445          *
446          * @name $.post
447          * @type jQuery
448          * @param String url The URL of the page to load.
449          * @param Hash params A set of key/value pairs that will be sent to the server.
450          * @param Function callback A function to be executed whenever the data is loaded.
451          * @cat AJAX
452          */
453         post: function( url, data, callback, type ) {
454                 // Build and start the HTTP Request
455                 jQuery.ajax( "POST", url, jQuery.param(data), function(r, status) {
456                         if ( callback ) callback( jQuery.httpData(r,type), status );
457                 });
458         },
459         
460         // timeout (ms)
461         timeout: 0,
462
463         /**
464          * Set the timeout of all AJAX requests to a specific amount of time.
465          * This will make all future AJAX requests timeout after a specified amount
466          * of time (the default is no timeout).
467          *
468          * @example $.ajaxTimeout( 5000 );
469          * @desc Make all AJAX requests timeout after 5 seconds.
470          *
471          * @test stop();
472          * var passed = 0;
473          * var timeout;
474          * $.ajaxTimeout(1000);
475          * var pass = function() {
476          *      passed++;
477          *      if(passed == 2) {
478          *              ok( true, 'Check local and global callbacks after timeout' );
479          *              clearTimeout(timeout);
480          *      $('#main').unbind("ajaxError");
481          *              start();
482          *      }
483          * };
484          * var fail = function() {
485          *      ok( false, 'Check for timeout failed' );
486          *      start();
487          * };
488          * timeout = setTimeout(fail, 1500);
489          * $('#main').ajaxError(pass);
490          * $.ajax({
491          *   type: "GET",
492          *   url: "data/name.php?wait=5",
493          *   error: pass,
494          *   success: fail
495          * });
496          *
497          * @test stop(); $.ajaxTimeout(50);
498          * $.ajax({
499          *   type: "GET",
500          *   timeout: 5000,
501          *   url: "data/name.php?wait=1",
502          *   error: function() {
503          *         ok( false, 'Check for local timeout failed' );
504          *         start();
505          *   },
506          *   success: function() {
507          *     ok( true, 'Check for local timeout' );
508          *     start();
509          *   }
510          * });
511          * 
512          *
513          * @name $.ajaxTimeout
514          * @type jQuery
515          * @param Number time How long before an AJAX request times out.
516          * @cat AJAX
517          */
518         ajaxTimeout: function(timeout) {
519                 jQuery.timeout = timeout;
520         },
521
522         // Last-Modified header cache for next request
523         lastModified: {},
524         
525         /**
526          * Load a remote page using an HTTP request. This function is the primary
527          * means of making AJAX requests using jQuery. $.ajax() takes one property,
528          * an object of key/value pairs, that're are used to initalize the request.
529          *
530          * These are all the key/values that can be passed in to 'prop':
531          *
532          * (String) type - The type of request to make (e.g. "POST" or "GET").
533          *
534          * (String) url - The URL of the page to request.
535          * 
536          * (String) data - A string of data to be sent to the server (POST only).
537          *
538          * (String) dataType - The type of data that you're expecting back from
539          * the server (e.g. "xml", "html", "script", or "json").
540          *
541          * (Boolean) ifModified - Allow the request to be successful only if the
542          * response has changed since the last request, default is false, ignoring
543          * the Last-Modified header
544          *
545          * (Number) timeout - Local timeout to override global timeout, eg. to give a
546          * single request a longer timeout while all others timeout after 1 seconds,
547          * see $.ajaxTimeout
548          *
549          * (Boolean) global - Wheather to trigger global AJAX event handlers for
550          * this request, default is true. Set to true to prevent that global handlers
551          * like ajaxStart or ajaxStop are triggered.
552          *
553          * (Function) error - A function to be called if the request fails. The
554          * function gets passed two arguments: The XMLHttpRequest object and a
555          * string describing the type of error that occurred.
556          *
557          * (Function) success - A function to be called if the request succeeds. The
558          * function gets passed one argument: The data returned from the server,
559          * formatted according to the 'dataType' parameter.
560          *
561          * (Function) complete - A function to be called when the request finishes. The
562          * function gets passed two arguments: The XMLHttpRequest object and a
563          * string describing the type the success of the request.
564          *
565          * @example $.ajax({
566          *   type: "GET",
567          *   url: "test.js",
568          *   dataType: "script"
569          * })
570          * @desc Load and execute a JavaScript file.
571          *
572          * @example $.ajax({
573          *   type: "POST",
574          *   url: "some.php",
575          *   data: "name=John&location=Boston",
576          *   success: function(msg){
577          *     alert( "Data Saved: " + msg );
578          *   }
579          * });
580          * @desc Save some data to the server and notify the user once its complete.
581          *
582          * @test stop();
583          * $.ajax({
584          *   type: "GET",
585          *   url: "data/name.php?name=foo",
586          *   success: function(msg){
587          *     ok( msg == 'bar', 'Check for GET' );
588          *     start();
589          *   }
590          * });
591          *
592          * @test stop();
593          * $.ajax({
594          *   type: "POST",
595          *   url: "data/name.php",
596          *   data: "name=peter",
597          *   success: function(msg){
598          *     ok( msg == 'pan', 'Check for POST' );
599          *     start();
600          *   }
601          * });
602          *
603          * @name $.ajax
604          * @type jQuery
605          * @param Hash prop A set of properties to initialize the request with.
606          * @cat AJAX
607          */
608         ajax: function( type, url, data, ret, ifModified ) {
609                 // If only a single argument was passed in,
610                 // assume that it is a object of key/value pairs
611                 if ( !url ) {
612                         ret = type.complete;
613                         var success = type.success;
614                         var error = type.error;
615                         var dataType = type.dataType;
616                         var global = typeof type.global == "boolean" ? type.global : true;
617                         var timeout = typeof type.timeout == "number" ? type.timeout : jQuery.timeout;
618                         var ifModified = type.ifModified || false;
619                         data = type.data;
620                         url = type.url;
621                         type = type.type;
622                 }
623                 
624                 // Watch for a new set of requests
625                 if ( global && ! jQuery.active++ )
626                         jQuery.event.trigger( "ajaxStart" );
627
628                 var requestDone = false;
629         
630                 // Create the request object
631                 var xml = new XMLHttpRequest();
632         
633                 // Open the socket
634                 xml.open(type || "GET", url, true);
635                 
636                 // Set the correct header, if data is being sent
637                 if ( data )
638                         xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
639                 
640                 // Set the If-Modified-Since header, if ifModified mode.
641                 if ( ifModified )
642                         xml.setRequestHeader("If-Modified-Since",
643                                 jQuery.lastModified[url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
644                 
645                 // Set header so the called script knows that it's an XMLHttpRequest
646                 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
647         
648                 // Make sure the browser sends the right content length
649                 if ( xml.overrideMimeType )
650                         xml.setRequestHeader("Connection", "close");
651                 
652                 // Wait for a response to come back
653                 var onreadystatechange = function(istimeout){
654                         // The transfer is complete and the data is available, or the request timed out
655                         if ( xml && (xml.readyState == 4 || istimeout == "timeout") ) {
656                                 requestDone = true;
657
658                                 var status = jQuery.httpSuccess( xml ) && istimeout != "timeout" ?
659                                         ifModified && jQuery.httpNotModified( xml, url ) ? "notmodified" : "success" : "error";
660                                 
661                                 // Make sure that the request was successful or notmodified
662                                 if ( status != "error" ) {
663                                         // Cache Last-Modified header, if ifModified mode.
664                                         var modRes;
665                                         try {
666                                                 modRes = xml.getResponseHeader("Last-Modified");
667                                         } catch(e) {} // swallow exception thrown by FF if header is not available
668                                         
669                                         if ( ifModified && modRes )
670                                                 jQuery.lastModified[url] = modRes;
671                                         
672                                         // If a local callback was specified, fire it
673                                         if ( success )
674                                                 success( jQuery.httpData( xml, dataType ), status );
675                                         
676                                         // Fire the global callback
677                                         if( global )
678                                                 jQuery.event.trigger( "ajaxSuccess" );
679                                 
680                                 // Otherwise, the request was not successful
681                                 } else {
682                                         // If a local callback was specified, fire it
683                                         if ( error ) error( xml, status );
684                                         
685                                         // Fire the global callback
686                                         if( global )
687                                                 jQuery.event.trigger( "ajaxError" );
688                                 }
689                                 
690                                 // The request was completed
691                                 if( global )
692                                         jQuery.event.trigger( "ajaxComplete" );
693                                 
694                                 // Handle the global AJAX counter
695                                 if ( global && ! --jQuery.active )
696                                         jQuery.event.trigger( "ajaxStop" );
697         
698                                 // Process result
699                                 if ( ret ) ret(xml, status);
700                                 
701                                 // Stop memory leaks
702                                 xml.onreadystatechange = function(){};
703                                 xml = null;
704                                 
705                         }
706                 };
707                 xml.onreadystatechange = onreadystatechange;
708                 
709                 // Timeout checker
710                 if(timeout > 0)
711                         setTimeout(function(){
712                                 // Check to see if the request is still happening
713                                 if (xml) {
714                                         // Cancel the request
715                                         xml.abort();
716
717                                         if ( !requestDone ) onreadystatechange( "timeout" );
718
719                                         // Clear from memory
720                                         xml = null;
721                                 }
722                         }, timeout);
723                 
724                 // Send the data
725                 xml.send(data);
726         },
727         
728         // Counter for holding the number of active queries
729         active: 0,
730         
731         // Determines if an XMLHttpRequest was successful or not
732         httpSuccess: function(r) {
733                 try {
734                         return !r.status && location.protocol == "file:" ||
735                                 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
736                                 jQuery.browser.safari && r.status == undefined;
737                 } catch(e){}
738
739                 return false;
740         },
741
742         // Determines if an XMLHttpRequest returns NotModified
743         httpNotModified: function(xml, url) {
744                 try {
745                         var xmlRes = xml.getResponseHeader("Last-Modified");
746
747                         // Firefox always returns 200. check Last-Modified date
748                         return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
749                                 jQuery.browser.safari && xml.status == undefined;
750                 } catch(e){}
751
752                 return false;
753         },
754         
755         /* Get the data out of an XMLHttpRequest.
756          * Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
757          * otherwise return plain text.
758          * (String) data - The type of data that you're expecting back,
759          * (e.g. "xml", "html", "script")
760          */
761         httpData: function(r,type) {
762                 var ct = r.getResponseHeader("content-type");
763                 var data = !type && ct && ct.indexOf("xml") >= 0;
764                 data = type == "xml" || data ? r.responseXML : r.responseText;
765
766                 // If the type is "script", eval it
767                 if ( type == "script" ) eval.call( window, data );
768
769                 // Get the JavaScript object, if JSON is used.
770                 if ( type == "json" ) eval( "data = " + data );
771
772                 return data;
773         },
774         
775         // Serialize an array of form elements or a set of
776         // key/values into a query string
777         param: function(a) {
778                 var s = [];
779                 
780                 // If an array was passed in, assume that it is an array
781                 // of form elements
782                 if ( a.constructor == Array || a.jquery ) {
783                         // Serialize the form elements
784                         for ( var i = 0; i < a.length; i++ )
785                                 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
786                         
787                 // Otherwise, assume that it's an object of key/value pairs
788                 } else {
789                         // Serialize the key/values
790                         for ( var j in a )
791                                 s.push( j + "=" + encodeURIComponent( a[j] ) );
792                 }
793                 
794                 // Return the resulting serialization
795                 return s.join("&");
796         }
797
798 });