var ajax = function()
{
this.page; // page to request the petition, default: the same page (window.location)
this.method; // ajax method: post or get, post by default
this.charset; // charset utf-8 by default
this.response; // response method, XML or Text, Text by default
this.query; // GET or POST query separated by & blank for default
this.async; // ajax connection method, true by default (firefox needs true to work)
this.getSeparator; // pagename and parameters separator ? by default
// request public method
ajax.prototype.request = function()
{
var a = new xmlhttp(); // get xmlhttp object
if(a) // if browser support ajax
{
var t = this;
setDefault.call(this); // set default properties
var b = headers(t.method, t.charset); // set get/post different headers
petitionConstruct.call(this); // construct get/post different properties
t.beforeReq(); // Method to do before all process
a.open(t.method,t.page,t.async); // open ajax petition
a.setRequestHeader('Content-Type', b); // set headers ALWAYS after OPEN
a.onreadystatechange = function() // get petition changestates
{
state(t, a);
};
a.send(t.query); // send get/post query
}
};
// public methods to redefine: w3schools.com
ajax.prototype.beforeReq = function(){}; // method to do before all process
ajax.prototype.reqSetUp = function(){}; // method to do when The request has been set up
ajax.prototype.reqSend = function(){}; // method to do when The request has been sent
ajax.prototype.reqInProcess = function(){}; // method to do when The request is in process
ajax.prototype.reqComplete = function(t){}; // method to do when The request is complete
ajax.prototype.reqError = function(){}; // method to do when The request return error
// private method to set default properties
var setDefault = function()
{
if(!this.method || this.method.toLowerCase()!='get') this.method='post';
if(!this.page) this.page=window.location;
if(!this.async || this.async!=false) this.async=true;
if(!this.charset) this.charset='utf-8';
if(!this.response || this.response.toLowerCase()!='xml') this.response='txt';
if(!this.query) this.query='';
if(typeof(this.getSeparator)=='undefined') this.getSeparator='?';
};
// private method to set get/post headers
var headers = function(m, c)
{
var a = '';
if(m.toLowerCase()=='post')
a = a + "application/x-www-form-urlencoded;"; // post Header
a = a + "charset="+c;
return a;
};
// private method set get/post petition properties
var petitionConstruct = function()
{
// DEFAULT POST example page = index.php and query = a=hello, php -> $_POST['hello']
if(this.method!='post')// GET example page = index.php?a=hello and query = '', php -> $_GET['hello']
{
this.page = this.page+this.getSeparator+this.query;
this.query = '';
}
};
// private method to get ajax petition state
var state = function(t, a)
{
switch(a.readyState)
{
case 1: // if readystate is 1 The request has been set up
t.reqSetUp();
break;
case 2: // if readystate is 2 The request has been sent
t.reqSend();
break;
case 3: // if readystate is 3 The request is in process
t.reqInProcess();
break;
case 4: // if readystate is 4 the request is complete
if(a.status==200) // if status is 200 petition OK
{
if (t.response == 'txt') // if get plain text
t.reqComplete(a.responseText); // execute complete method
else // else get XML
t.reqComplete(a.responseXML); // execute complete method
}
else
t.reqError(); // if error occurred execute error method
break;
}
};
// private method get xmlhttp object
var xmlhttp = function()
{
var a;try{a = new XMLHttpRequest();}
catch(e){try{a = new ActiveXObject('Msxml2.XMLHTTP');}
catch(e){try{a = new ActiveXObject('Microsoft.XMLHTTP');}
catch(e){alert('Your browser doesn\'t support ajax');a=false;}
}}return a;
};
};