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');
94 xhr.setRequestHeader('Content-length', code.length);
95 xhr.setRequestHeader('Connection', 'close');
99 this.cancel = function()
101 this._xmlHttp.onreadystatechange = function(){};
102 this._xmlHttp.abort();
105 this.send_form = function(form,callback,extra_values)
109 for (var i = 0; i < form.elements.length; i++)
111 var e = form.elements[i];
115 code += (code ? '&' : '') +
116 form.elements[i].name + '=' + encodeURIComponent(
117 e.options[e.selectedIndex].value
122 for (var j = 0; j < e.length; j++)
124 code += (code ? '&' : '') +
125 e[j].name + '=' + encodeURIComponent(e[j].value);
130 code += (code ? '&' : '') +
131 e.name + '=' + encodeURIComponent(e.value);
135 if (typeof extra_values == 'object')
136 for (var key in extra_values)
137 code += (code ? '&' : '') +
138 key + '=' + encodeURIComponent(extra_values[key]);
141 (form.method == 'get')
142 ? this.get(form.getAttribute('action'), code, callback)
143 : this.post(form.getAttribute('action'), code, callback)
147 this._encode = function(obj)
149 obj = obj ? obj : { };
150 obj['_'] = Math.random();
152 if (typeof obj == 'object')
158 code += (code ? '&' : '') +
159 k + '=' + encodeURIComponent(obj[k]);
168 XHR.get = function(url, data, callback)
170 (new XHR()).get(url, data, callback);
173 XHR.poll = function(interval, url, data, callback)
175 if (isNaN(interval) || interval < 1)
182 XHR._r = function() {
183 for (var i = 0, e = XHR._q[0]; i < XHR._q.length; e = XHR._q[++i])
185 if (!(XHR._t % e.interval) && !e.xhr.busy())
186 e.xhr.get(e.url, e.data, e.callback);
204 XHR.halt = function()
208 /* show & set poll indicator */
210 document.getElementById('xhr_poll_status').style.display = '';
211 document.getElementById('xhr_poll_status_on').style.display = 'none';
212 document.getElementById('xhr_poll_status_off').style.display = '';
215 window.clearInterval(XHR._i);
222 if (XHR._r && !XHR._i)
224 /* show & set poll indicator */
226 document.getElementById('xhr_poll_status').style.display = '';
227 document.getElementById('xhr_poll_status_on').style.display = '';
228 document.getElementById('xhr_poll_status_off').style.display = 'none';
231 /* kick first round manually to prevent one second lag when setting up
232 * the poll interval */
234 XHR._i = window.setInterval(XHR._r, 1000);
238 XHR.running = function()
240 return !!(XHR._r && XHR._i);