Usage:
New Ajax (). Request (Url,cmd,async,method,poststring,title)
Parameters:
URL: Request page URL (required)
CMD: Return value processing function (required)
Async: Asynchronous, (Ture|false), default True
Method: Request mode, (Post|get), default get
Poststring: The request is in the form of post, the requested content
Title: Request Content Headers
Copy Code code as follows:
Ajax Encapsulation 2007-3-13
function Createxmlhttprequest () {
try {
if (window. XMLHttpRequest) {
return new XMLHttpRequest ();
}
else if (window. ActiveXObject) {
return new ActiveXObject ("Microsoft.XMLHTTP");
}
}
catch (E) {alert ("XMLHttpRequest object cannot be created!) Please check IE security settings! ");}
}
function Messagediv (t)
{
var v = document.createelement ("<div>");
v.innerhtml = "<table style=\" width:300px;\ "id=message>" +
"<tr style=\" Font-size:12px;background-color: #EEEEff; color: #227933; height:20px\ ">" +
"<td style=\" padding:2px;border-top:1px solid #E1E1E1 border-left:1px solid #E1E1E1 border-bottom:1px Solid 818181;border-right:1px solid #A1A1A1 \ ">" +
"<nobr> Connection not initialized ... </span></ Nobr></td></tr></table> ";
var L = document.getelementsbyname ("message"). Length;
V.style.csstext = "Position:absolute;bottom:" + (l*24) + "Px;left:0px;display:none";
Document.body.appendChild (v);
This.clear = function () {
Document.body.removeChild (v);
var msg = document.getelementsbyname ("message");
for (Var i=0;i<msg.length;i++) {
Msg[i].parentnode.style.csstext = "Position:absolute;bottom:" + (i*24) + "px;left:0px";
}
}
This.showmsg = function (s) {
V.style.display = "";
v.all.span1.innerhtml = s;
}
}
function Ajax () {
var x = new Createxmlhttprequest ();
This. Request = function (url,cmd,async,method,poststring,title) {
if (method!= "post") method = "POST"; else method = ' get ';
if (async!=true) async = true; else Async = false;
if (typeof (Poststring)!= "string") poststring= "";
if (typeof (title)!= "string") title= "fetching data"; Else title= "is getting" + title;
var MsgBox = new Messagediv (title);
X.onreadystatechange = function ()
{
if (async) switch (x.readystate) {
Case 1:
Msgbox.showmsg ("Initializing connection ...");
Return
Case 2:
Msgbox.showmsg ("Sending data ...");
Return
Case 3:
Msgbox.showmsg ("receiving data ...");
Return
Case 4:
Msgbox.showmsg ("Data reception complete ...");
if (X.status = = 200) {
CMD (x.responsetext);
Msgbox.clear ();
}
else {
Msgbox.showmsg ("Request failed," + X.statustext + "(" + X.status +) ");
SetTimeout (msgbox.clear,3000);
}
Return
}
}
X.open (Method,url,async);
if (method== "post") {msgbox.showmsg ("receiving data ..."); X.send (poststring);} else X.send ();
if (!async) {
Msgbox.showmsg ("Data reception complete ...");
CMD (x.responsetext);
Msgbox.clear ();
}
}
}