3 Contains the <Ajax> class. Also contains methods to generate querystings from forms and Objects.
6 Loosely based on the version from prototype.js <http://prototype.conio.net>
14 An Ajax class, For all your asynchronous needs. Inherits methods, properties and options from <XHR>.
17 url - the url pointing to the server-side script.
18 options - optional, an object containing options.
21 data - you can write parameters here. Can be a querystring, an object or a Form element.
22 onComplete - function to execute when the ajax request completes.
23 update - $(element) to insert the response text of the XHR into, upon completion of the request.
24 evalScripts - boolean; default is false. Execute scripts in the response text onComplete. When the response is javascript the whole response is evaluated.
25 evalResponse - boolean; default is false. Force global evalulation of the whole response, no matter what content-type it is.
28 >var myAjax = new Ajax(url, {method: 'get'}).request();
31 var Ajax = XHR.extend({
36 onComplete: Class.empty,
41 initialize: function(url, options){
42 this.addEvent('onSuccess', this.onComplete);
43 this.setOptions(options);
44 this.options.data = this.options.data || this.options.postBody;
45 if (!['post', 'get'].contains(this.options.method)){
46 this._method = '_method=' + this.options.method;
47 this.options.method = 'post';
50 this.setHeader('X-Requested-With', 'XMLHttpRequest');
51 this.setHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
55 onComplete: function(){
56 if (this.options.update) $(this.options.update).empty().setHTML(this.response.text);
57 if (this.options.evalScripts || this.options.evalResponse) this.evalScripts();
58 this.fireEvent('onComplete', [this.response.text, this.response.xml], 20);
63 Executes the ajax request.
66 >var myAjax = new Ajax(url, {method: 'get'});
71 >new Ajax(url, {method: 'get'}).request();
74 request: function(data){
75 data = data || this.options.data;
77 case 'element': data = $(data).toQueryString(); break;
78 case 'object': data = Object.toQueryString(data);
80 if (this._method) data = (data) ? [this._method, data].join('&') : this._method;
81 return this.send(this.url, data);
86 Executes scripts in the response text
89 evalScripts: function(){
90 if (this.options.evalResponse || /(ecma|java)script/.test(this.getHeader('Content-type'))) var scripts = this.response.text;
92 var script, scripts = [], regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
93 while ((script = regexp.exec(this.response.text))) scripts.push(script[1]);
94 scripts = scripts.join('\n');
96 if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
101 Returns the given response header or null
104 getHeader: function(name) {
105 try {return this.transport.getResponseHeader(name);} catch(e){};
111 /* Section: Object related Functions */
114 Function: Object.toQueryString
115 Generates a querystring from key/pair values in an object
118 source - the object to generate the querystring from.
124 >Object.toQueryString({apple: "red", lemon: "yellow"}); //returns "apple=red&lemon=yellow"
127 Object.toQueryString = function(source){
128 var queryString = [];
129 for (var property in source) queryString.push(encodeURIComponent(property) + '=' + encodeURIComponent(source[property]));
130 return queryString.join('&');
135 Custom class to allow all of its methods to be used with any DOM element via the dollar function <$>.
142 Sends a form with an ajax post request
145 options - option collection for ajax request. See <Ajax> for the options list.
148 The Ajax Class Instance
152 <form id="myForm" action="submit.php">
153 <input name="email" value="bob@bob.com">
154 <input name="zipCode" value="90210">
162 send: function(options){
163 return new Ajax(this.getProperty('action'), $merge({postBody: this.toQueryString()}, options, {method: 'post'})).request();