SarahAjax Javascript Ajax Class
June 3, 2008
Okay, here is my SarahAjax Javascript Class. You can use it without hassle!
You can either inherit and ‘action’ function or by prototyping a function named ‘action’. ‘action’ function will be called when the request to the resource is successful.
This is SarahAjax Class.
// 6/2/2008 HanaDaddy hanadaddy at gmail.com /* SarahAjax is a very simple ajax class. * url: target url to call. It is usally dynamic script or a simple page return xml or text. */ function SarahAjax(url){ this.url=url; this.reqobj ; this.method="GET"; this.async= true; //if false , the connection will be synchronous ( you have to wait for the reply) //this.action = function (obj) { } // Success reply handler. : youre responsibilty //this.error = function (obj) { } // Error reply handler : your responsiblity to implement. this.sendRequest = function() { if(this.reqobj == null){ if (window.XMLHttpRequest) { this.reqobj = new XMLHttpRequest(); } else if (window.ActiveXObject) { this.reqobj = new ActiveXObject("Microsoft.XMLHTTP"); } } if (!this.action){ alert("SaraAjax:Action member function is not defined! We need the reply handler!"); return; } this.reqobj.open(this.method, this.url, this.async); var sajaxreq = this; // this is necessary to access myself in the callback function this.reqobj.onreadystatechange = function() { sajaxreq.callback(); } this.reqobj.send(null); } this.callback = function () { if (this.reqobj.readyState == 4) { if (this.reqobj.status == 200) { //alert("reply is back?"+this.reqobj.responseText); this.action(this.reqobj); }else{ //alert("There was a problem retrieving data:\n" + this.reqobj.statusText); if (this.error){ this.error(this.reqobj); } } } } }