Home >Web Front-end >JS Tutorial >A flawless way to instantiate XMLHttpRequest in javascript_javascript tips

A flawless way to instantiate XMLHttpRequest in javascript_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:18:461088browse
Copy code The code is as follows:

function getHTTPRequest()
{
var xhr = false ;
if (window.XMLHttpRequest)
xhr = new XMLHttpRequest(); //Browsers except IE
else if (window.ActiveXObject)
{
try
{
xhr = new ActiveXObject("Msxm12.XMLHTTP");//The latest version of ActiveX object
}
catch(e)
{
try
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xhr = false;
}
}
}
}

The following is an application example:
Copy the code The code is as follows:

//Complete GET request
var request = getHTTPRequest();
if(request)
{
request.onreadystatechange = dosomething;
request.open("GET"," file.doc",true);
request.send(null);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn