Código javascript
:
Ver originalvar 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.loading(); // Loading method ON
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
}
else
alert('Your browser doesn\'t support ajax');
};
// public methods to redefine
ajax.prototype.loading = function(){};
ajax.prototype.complete = function(t){};
ajax.prototype.error = function(){};
// private method to set default properties
var setDefault = function()
{
if(!this.method) this.method='post';
if(!this.page) this.page=window.location;
if(!this.async) this.async=true;
if(!this.charset) this.charset='utf-8';
if(!this.response) this.response='txt';
if(!this.query) this.query='';
if(!this.getSeparator) 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 = '';
}
};
var state = function(t, a)
{
switch(a.readyState)
{
case 4: // if changestate is 4 the request is complete
if(a.status==200) // if status is 200 petition OK
{
if(t.response.toLowerCase()=='xml') // if XML response get XML
t.complete(a.responseXML); // execute complete method
else // else get plain text
t.complete(a.responseText); // execute complete method
}
else
t.error(); // 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){a=false;}}}return a;
};
};
he cambiado el:
Código javascript
:
Ver originala.onreadystatechange = function() // get petition changestates
y le he añadido dentro una funcion privada para que este un poco mas organizado:
Código javascript
:
Ver originala.onreadystatechange = function() // get petition changestates
{
state(t, a);
};
...
var state = function(t, a)
{
switch(a.readyState)
{
case 4: // if changestate is 4 the request is complete
if(a.status==200) // if status is 200 petition OK
{
if(t.response.toLowerCase()=='xml') // if XML response get XML
t.complete(a.responseXML); // execute complete method
else // else get plain text
t.complete(a.responseText); // execute complete method
}
else
t.error(); // if error occurred execute error method
break;
}
};