ajax.js
Código javascript:
Ver original
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.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 { 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; } }; 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 = ''; } }; // 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; }; };
Un ejemplo de uso sería:
index.php
Código PHP:
<?php
if($_GET['a']=='a')
{
echo "GET: AAAAAAAAAAAAAAAAAAAAAAAAAAA";
exit();
}
elseif($_POST['a']=='a')
{
echo "POST: AAAAAAAAAAAAAAAAAAAAAAAAAAA";
exit();
}
?>
Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es"> <head> <title>YEAH</title> <script type="text/javascript" src="js/ajax.js"> </script> <script type="text/javascript" src="js/main.js"></script> </head> <body> <div id="post">post content</div> <div id="get">get content</div> </body> </html>
Código javascript:
Ver original
window.onload = function() { // simple EXAMPLE: POST var b = new ajax(); b.query = 'a=a'; b.loading = function() { var a = document.getElementById('post').innerHTML='loading...'; }; b.complete = function(t) { window.setTimeout(function() { var a = document.getElementById('post').innerHTML=t; }, 1500 ); }; b.error = function() { alert('Error POST petition'); }; b.request(); // advance example GET var c = new ajax(); c.page = 'index.php'; c.method = 'get'; c.charset = 'utf-8'; c.response = 'txt'; c.query = 'a=a'; c.async = true; c.getSeparator = '?'; c.loading = function() { var a = document.getElementById('get').innerHTML='loading...'; }; c.complete = function(t) { window.setTimeout(function() { var a = document.getElementById('get').innerHTML=t; }, 3000 ); }; c.error = function() { alert('Error GET petition'); }; c.request(); };