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)
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);
59 xhr.onreadystatechange = function()
61 if (xhr.readyState == 4) {
63 if (xhr.getResponseHeader("Content-Type") == "application/json") {
65 json = eval('(' + xhr.responseText + ')');
79 this.post = function(url,data,callback)
83 var xhr = this._xmlHttp;
84 var code = this._encode(data);
86 xhr.onreadystatechange = function()
88 if (xhr.readyState == 4)
92 xhr.open('POST', url, true);
93 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
97 this.cancel = function()
99 this._xmlHttp.onreadystatechange = function(){};
100 this._xmlHttp.abort();
103 this.send_form = function(form,callback,extra_values)
107 for (var i = 0; i < form.elements.length; i++)
109 var e = form.elements[i];
113 code += (code ? '&' : '') +
114 form.elements[i].name + '=' + encodeURIComponent(
115 e.options[e.selectedIndex].value
120 for (var j = 0; j < e.length; j++)
122 code += (code ? '&' : '') +
123 e[j].name + '=' + encodeURIComponent(e[j].value);
128 code += (code ? '&' : '') +
129 e.name + '=' + encodeURIComponent(e.value);
133 if (typeof extra_values == 'object')
134 for (var key in extra_values)
135 code += (code ? '&' : '') +
136 key + '=' + encodeURIComponent(extra_values[key]);
139 (form.method == 'get')
140 ? this.get(form.getAttribute('action'), code, callback)
141 : this.post(form.getAttribute('action'), code, callback)
145 this._encode = function(obj)
147 obj = obj ? obj : { };
148 obj['_'] = Math.random();
150 if (typeof obj == 'object')
156 code += (code ? '&' : '') +
157 k + '=' + encodeURIComponent(obj[k]);
166 XHR.get = function(url, data, callback)
168 (new XHR()).get(url, data, callback);
171 XHR.poll = function(interval, url, data, callback)
173 if (isNaN(interval) || interval < 1)
180 XHR._r = function() {
181 for (var i = 0, e = XHR._q[0]; i < XHR._q.length; e = XHR._q[++i])
183 if (!(XHR._t % e.interval) && !e.xhr.busy())
184 e.xhr.get(e.url, e.data, e.callback);
202 XHR.halt = function()
206 /* show & set poll indicator */
208 document.getElementById('xhr_poll_status').style.display = '';
209 document.getElementById('xhr_poll_status_on').style.display = 'none';
210 document.getElementById('xhr_poll_status_off').style.display = '';
213 window.clearInterval(XHR._i);
220 if (XHR._r && !XHR._i)
222 /* show & set poll indicator */
224 document.getElementById('xhr_poll_status').style.display = '';
225 document.getElementById('xhr_poll_status_on').style.display = '';
226 document.getElementById('xhr_poll_status_off').style.display = 'none';
229 /* kick first round manually to prevent one second lag when setting up
230 * the poll interval */
232 XHR._i = window.setInterval(XHR._r, 1000);
236 XHR.running = function()
238 return !!(XHR._r && XHR._i);