Nowadays, Ajax technology is becoming more and more popular, and many server-side frameworks have emerged. Ajax can be used like server-side code, but I still think it is a little troublesome, it is better to use JavaScript directly for simplicity and convenience. I found a piece of code from the Internet and implemented the Ajax connection pool. I think it is good to share it with everyone.
VaR request = new function ()
{
This. Pool = new array ();
This. getxmlhttp = function (chunnel)
{
If (Chunnel! = NULL)
{
For (VAR a = 0; A <this. Pool. length; A ++)
{
If (this. Pool [a] ["Chunnel"] = chunnel)
{
If (this. Pool [a] ["OBJ"]. readystate = 0 | this. Pool [a] ["OBJ"]. readystate = 4)
{
Return this. Pool [a] ["OBJ"];
}
Else
{
Return "busy ";
}
}
}
This. Pool [This. Pool. Length] = new array ();
This. Pool [This. Pool. Length-1] ["OBJ"] = This. createxmlhttp ();
This. Pool [This. Pool. Length-1] ["Chunnel"] = chunnel;
Return this. Pool [This. Pool. Length-1] ["OBJ"];
}
For (VAR I = 0; I <this. Pool. length; I ++)
{
If (this. Pool [I] ["OBJ"]. readystate = 0 | this. Pool [I] ["OBJ"]. readystate = 4)
{
Return this. Pool [I] ["OBJ"];
}
}
This. Pool [This. Pool. Length] = new array ();
This. Pool [This. Pool. Length-1] ["OBJ"] = This. createxmlhttp ();
This. Pool [This. Pool. Length-1] ["Chunnel"] = "";
Return this. Pool [This. Pool. Length-1] ["OBJ"];
}
This. createxmlhttp = function ()
{
If (window. XMLHttpRequest)
{
VaR xmlobj = new XMLHttpRequest ();
}
Else
{
VaR MSXML = ['Microsoft. xmlhttp', 'msxml2. xmlhttp.5.0 ', 'msxml2. xmlhttp.4.0', 'msxml2. xmlhttp.3.0 ', 'msxml2. xmlhttp'];
For (VAR n = 0; n <MSXML. length; n ++)
{
Try
{
VaR xmlobj = new activexobject (MSXML [N]);
Break;
}
Catch (E)
{
}
}
}
Return xmlobj;
}
This. resend = function (URL, Data, callback, Chunnel)
{
VaR objxmlhttp = This. getxmlhttp (chunnel)
If (typeof (objxmlhttp )! = "Object ")
{
Return;
}
URL + = (URL. indexof ("? ")> = 0 )? "& Nowtime =" + new date (). gettime ():"? Nowtime = "+ new date (). gettime ();
If (Data = "")
{
Objxmlhttp. Open ('get', URL, true );
Objxmlhttp. Send ('');
}
Else
{
Objxmlhttp. Open ('post', URL, true );
Objxmlhttp. setRequestHeader ("Content-Length", Data. Length );
Objxmlhttp. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
Objxmlhttp. Send (data );
}
If (typeof (callback) = "function ")
{
Objxmlhttp. onreadystatechange = function ()
{
If (objxmlhttp. readystate = 4)
{
If (objxmlhttp. Status = 200 | objxmlhttp. Status = 304)
{
Callback (objxmlhttp)
}
Else
{
Alert ("error loading Page \ n" + objxmlhttp. Status + ":" + objxmlhttp. statustext );
}
}
}
}
}
}