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