// Use literal syntax to define an object: XMLHttpvarXMLHttp {// define the first attribute, which is used to cache the XMLHttpRequest object array XMLHttpRequestPool: [], // The first method of the object, this method is used to return an XMLHttpRequest object... synta // use literal syntax to define an object: XMLHttp
Var XMLHttp =
{
// Define the first attribute, which is used to cache the array of the XMLHttpRequest object
XMLHttpRequestPool: [],
// The first method of the object. This method is used to return an XMLHttpRequest object.
GetInstance: function ()
{
// Retrieve an idle XMLHttpRequest from the XMLHttpRequest object pool
For (var I = 0; I <this. XMLHttpRequestPool. length; I ++)
{
// If the readyState of XMLHttpReuqest is 0 or 4,
// Both indicate that the current XMLHttpRequest object is an idle object.
If (this. XMLHttpRequestPool [I]. readyState = 0 |
This. XMLHttpRequestPool [I]. readyState = 4)
{
Return this. XMLHttpRequestPool [I];
}
}
// If there is no idle space, a new XMLHttpRequest object will be created again.
This. XMLHttpRequestPool [this. XMLHttpRequestPool. length]
= This. createXMLHttpRequest ();
// Return the created XMLHttpRequest object
Return this. XMLHttpRequestPool [this. XMLHttpRequestPool. length-1];
},
// Create a new XMLHttpRequest object
CreateXMLHttpRequest: function ()
{
// For the DOM 2 standard browser
If (window. XMLHttpRequest)
{
Var objXMLHttp = new XMLHttpRequest ();
}
// For Internet Explorer
Else
{
// Set all XMLHTTP ActiveX Control built in Internet Explorer to an array
Var MSXML = ['msxml2. XMLHTTP.5.0 ', 'msxml2. XMLHTTP.4.0 ',
'Msxml2. XMLHTTP.3.0 ', 'msxml2. xmlhttp', 'Microsoft. xmlhttp'];
// Initialize the XMLHTTP control built in Internet Explorer in sequence and try to create the XMLHttpRequest object
For (var n = 0; n <MSXML. length; n ++)
{
Try
{
// If the XMLHttpRequest object can be created normally, use break to exit the loop
Var objXMLHttp = new ActiveXObject (MSXML [n]);
Break;
}
Catch (e)
{
}
}
}
// Some Mozilla versions do not have the readyState attribute
If (objXMLHttp. readyState = null)
{
// Directly set its readyState to 0
ObjXMLHttp. readyState = 0;
// For Browsers without the readyState attribute, associate the load action with the following function.
ObjXMLHttp. addEventListener ("load", function ()
{
// After loading data from the server, set the readyState status to 4.
ObjXMLHttp. readyState = 4;
If (typeof objXMLHttp. onreadystatechange = "function ")
{
ObjXMLHttp. onreadystatechange ();
}
}, False );
}
Return objXMLHttp;
},
// Define the third method of the object: send the request (method [POST, GET], address, Data, callback function)
SendRequest: function (method, url, data, callback)
{
Var objXMLHttp = this. getInstance ();
With (objXMLHttp)
{
Try
{
// Add an additional randnum request parameter to prevent response from the IE Cache Server
If (url. indexOf ("? ")> 0)
{
Url + = "& randnum =" + Math. random ();
}
Else
{
Url + = "? Randnum = "+ Math. random ();
}
// Open the connection to the server
Open (method, url, true );
// For post requests
If (method = "POST ")
{
// Set the Request Header
SetRequestHeader ('content-type ',
'Application/x-www-form-urlencoded ');
Send (data );
}
// For GET requests
If (method = "GET ")
{
Send (null );
}
// Set the callback function for status change
Onreadystatechange = function ()
{
// When the server completes, and receives a normal Server Response
If (objXMLHttp. readyState = 4 &&
(ObjXMLHttp. status = 200 |
ObjXMLHttp. status = 304 ))
{
// Call the callback function to handle the response when the response time is ripe.
Callback (objXMLHttp );
}
}
}
Catch (e)
{
Alert (e );
}
}
}
};
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service