106 lines
3.2 KiB
JavaScript
106 lines
3.2 KiB
JavaScript
/*
|
|
This program is free software. It comes without any warranty, to
|
|
the extent permitted by applicable law. You can redistribute it
|
|
and/or modify it under the terms of the Do What The Fuck You Want
|
|
To Public License, Version 2, as published by Sam Hocevar. See
|
|
http://sam.zoy.org/wtfpl/COPYING for more details.
|
|
*/
|
|
|
|
/*
|
|
httpobj: javascript class using XMLHttpRequest to send and retrieve info.
|
|
See /httpobj to get an example of the usage.
|
|
*/
|
|
//v.1.0
|
|
function httpobj() {
|
|
// Get XMLHttpRequest object
|
|
this.obj = this.getobj()
|
|
if (!this.obj) {
|
|
alert('Your browser does not support HTTP Requests! Get a new one (a browser, that is. Not a request).')
|
|
delete this
|
|
return false
|
|
}
|
|
|
|
// Define default variables
|
|
this.param = {}
|
|
this.method = 'POST'
|
|
|
|
// Fix for later on
|
|
this.obj.topelem = this
|
|
}
|
|
|
|
httpobj.prototype.getobj = function() {
|
|
// Try to get the XMLHttpRequest object
|
|
try { return new XMLHttpRequest() } catch (e) {}
|
|
try { return new ActiveXObject('Msxml2.XMLHTTP.6.0') } catch(e) {}
|
|
try { return new ActiveXObject('Msxml2.XMLHTTP.3.0') } catch(e) {}
|
|
try { return new ActiveXObject('Msxml2.XMLHTTP') } catch(e) {}
|
|
try { return new ActiveXObject('Microsoft.XMLHTTP') }
|
|
catch(e) {
|
|
return false // FAILURE
|
|
}
|
|
}
|
|
|
|
httpobj.prototype.send = function() {
|
|
/*
|
|
send(): Sends parametres (param) to url (url) and sets up function to
|
|
react on retrievement
|
|
*/
|
|
|
|
if (!this.url) {
|
|
// Exit function if either url or the function to be called is undefined
|
|
delete this
|
|
return false
|
|
}
|
|
|
|
// Transform param into string type ?foo=bar&hmm=aha
|
|
this.text_params = ''
|
|
if (this.param) {
|
|
for (var x in this.param)
|
|
this.text_params += '&' + x + '=' + this.param[x]
|
|
this.text_params = this.text_params.substr(1)
|
|
}
|
|
|
|
if (this.method.toLowerCase() == 'get') {
|
|
// In case of GET method, use '?'
|
|
var after_url
|
|
if (!this.param)
|
|
after_url = ''
|
|
else
|
|
after_url = '?' + this.text_params
|
|
|
|
this.obj.open('GET', this.url + after_url, true)
|
|
this.obj.send(null)
|
|
}
|
|
else {
|
|
// In case of POST method, use RequestHeader
|
|
this.obj.open('POST', this.url, true)
|
|
this.obj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
|
|
this.obj.setRequestHeader('Content-length', this.text_params.length)
|
|
this.obj.setRequestHeader('Connection', 'close')
|
|
this.obj.send(this.text_params)
|
|
}
|
|
|
|
// What to do when something happens
|
|
this.obj.onreadystatechange = function() {
|
|
// Only do something when it's complete
|
|
if (this.readyState == 4 || this.readyState == 'complete') {
|
|
// Define 3 variables to be used with the user-defined function (func)
|
|
var text, params, state
|
|
text = this.responseText
|
|
params = this.topelem.param // this.topelem = this.obj.topelem = this
|
|
|
|
// State 0 is good, state 1 is bad
|
|
state = 0
|
|
if (this.status != 200) {
|
|
state = 1
|
|
text = ''
|
|
}
|
|
|
|
// Call user-defined function
|
|
if (this.topelem.func)
|
|
eval(this.topelem.func)
|
|
delete this
|
|
}
|
|
}
|
|
return true // It has a chance of working..
|
|
} |