2 * xhr.js - XMLHttpRequest helper class
3 * (c) 2008-2010 Jo-Philipp Wich
8 this.reinit = function()
10 if (window.XMLHttpRequest) {
11 this._xmlHttp = new XMLHttpRequest();
13 else if (window.ActiveXObject) {
14 this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
17 alert("xhr.js: XMLHttpRequest is not supported by this browser!");
21 this.busy = function() {
25 switch (this._xmlHttp.readyState)
37 this.abort = function() {
39 this._xmlHttp.abort();
42 this.get = function(url,data,callback,timeout)
46 var xhr = this._xmlHttp;
47 var code = this._encode(data);
49 url = location.protocol + '//' + location.host + url;
52 if (url.substr(url.length-1,1) == '&')
58 xhr.timeout = timeout;
60 xhr.open('GET', url, true);
62 xhr.onreadystatechange = function()
64 if (xhr.readyState == 4) {
66 if (xhr.getResponseHeader("Content-Type") == "application/json") {
68 json = eval('(' + xhr.responseText + ')');
82 this.post = function(url,data,callback,timeout)
86 var xhr = this._xmlHttp;
87 var code = this._encode(data);
89 xhr.onreadystatechange = function()
91 if (xhr.readyState == 4)
96 xhr.timeout = timeout;
98 xhr.open('POST', url, true);
99 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
103 this.cancel = function()
105 this._xmlHttp.onreadystatechange = function(){};
106 this._xmlHttp.abort();
109 this.send_form = function(form,callback,extra_values)
113 for (var i = 0; i < form.elements.length; i++)
115 var e = form.elements[i];
119 code += (code ? '&' : '') +
120 form.elements[i].name + '=' + encodeURIComponent(
121 e.options[e.selectedIndex].value
126 for (var j = 0; j < e.length; j++)
128 code += (code ? '&' : '') +
129 e[j].name + '=' + encodeURIComponent(e[j].value);
134 code += (code ? '&' : '') +
135 e.name + '=' + encodeURIComponent(e.value);
139 if (typeof extra_values == 'object')
140 for (var key in extra_values)
141 code += (code ? '&' : '') +
142 key + '=' + encodeURIComponent(extra_values[key]);
145 (form.method == 'get')
146 ? this.get(form.getAttribute('action'), code, callback)
147 : this.post(form.getAttribute('action'), code, callback)
151 this._encode = function(obj)
153 obj = obj ? obj : { };
154 obj['_'] = Math.random();
156 if (typeof obj == 'object')
162 code += (code ? '&' : '') +
163 k + '=' + encodeURIComponent(obj[k]);
172 XHR.get = function(url, data, callback)
174 (new XHR()).get(url, data, callback);
177 XHR.poll = function(interval, url, data, callback, post)
179 if (isNaN(interval) || interval < 1)
186 XHR._r = function() {
187 for (var i = 0, e = XHR._q[0]; i < XHR._q.length; e = XHR._q[++i])
189 if (!(XHR._t % e.interval) && !e.xhr.busy())
190 e.xhr[post ? 'post' : 'get'](e.url, e.data, e.callback, e.interval * 1000 - 5);
211 XHR.stop = function(e)
213 for (var i = 0; XHR._q && XHR._q[i]; i++) {
214 if (XHR._q[i] === e) {
224 XHR.halt = function()
228 /* show & set poll indicator */
230 document.getElementById('xhr_poll_status').style.display = '';
231 document.getElementById('xhr_poll_status_on').style.display = 'none';
232 document.getElementById('xhr_poll_status_off').style.display = '';
235 window.clearInterval(XHR._i);
242 if (XHR._r && !XHR._i)
244 /* show & set poll indicator */
246 document.getElementById('xhr_poll_status').style.display = '';
247 document.getElementById('xhr_poll_status_on').style.display = '';
248 document.getElementById('xhr_poll_status_off').style.display = 'none';
251 /* kick first round manually to prevent one second lag when setting up
252 * the poll interval */
254 XHR._i = window.setInterval(XHR._r, 1000);
258 XHR.running = function()
260 return !!(XHR._r && XHR._i);