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) == '&')
57 xhr.open('GET', url, true);
60 xhr.timeout = timeout;
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)
95 xhr.open('POST', url, true);
98 xhr.timeout = timeout;
100 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
104 this.cancel = function()
106 this._xmlHttp.onreadystatechange = function(){};
107 this._xmlHttp.abort();
110 this.send_form = function(form,callback,extra_values)
114 for (var i = 0; i < form.elements.length; i++)
116 var e = form.elements[i];
120 code += (code ? '&' : '') +
121 form.elements[i].name + '=' + encodeURIComponent(
122 e.options[e.selectedIndex].value
127 for (var j = 0; j < e.length; j++)
129 code += (code ? '&' : '') +
130 e[j].name + '=' + encodeURIComponent(e[j].value);
135 code += (code ? '&' : '') +
136 e.name + '=' + encodeURIComponent(e.value);
140 if (typeof extra_values == 'object')
141 for (var key in extra_values)
142 code += (code ? '&' : '') +
143 key + '=' + encodeURIComponent(extra_values[key]);
146 (form.method == 'get')
147 ? this.get(form.getAttribute('action'), code, callback)
148 : this.post(form.getAttribute('action'), code, callback)
152 this._encode = function(obj)
154 obj = obj ? obj : { };
155 obj['_'] = Math.random();
157 if (typeof obj == 'object')
163 code += (code ? '&' : '') +
164 k + '=' + encodeURIComponent(obj[k]);
173 XHR.get = function(url, data, callback)
175 (new XHR()).get(url, data, callback);
178 XHR.poll = function(interval, url, data, callback, post)
180 if (isNaN(interval) || interval < 1)
187 XHR._r = function() {
188 for (var i = 0, e = XHR._q[0]; i < XHR._q.length; e = XHR._q[++i])
190 if (!(XHR._t % e.interval) && !e.xhr.busy())
191 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);
263 document.addEventListener('DOMContentLoaded', XHR.run);