三种方式获取XMLHttpRequest对象

2022-01-14,,,,

这篇文章主要介绍了获取XMLHttpRequest对象三种方式,需要的朋友可以参考下

获取XmlHttpRequest对象
复制代码 代码如下:
//1
function getXMLHttpRequest() {
var xmlHttpReq;
try { // Firefox, Opera 8.0+, Safari
xmlHttpReq = new XMLHttpRequest();
} catch (e) {
try {// Internet Explorer
xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
return xmlHttpReq;
}

//2
function getXMLHttpRequest() {
var xmlHttpReq = null;
if (window.ActiveXObject) {// Internet Explorer
xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");
} else if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}
return xmlHttpReq;
}

//3
function getXMLHttpRequest() {
var xmlHttpReq = null;
if (window.XMLHttpRequest) {// Mozilla Firefox, Opera 8.0+, Safari
xmlHttpReq = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {// Internet Explorer
try {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
try {// Internet Explorer
xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
}
}
}
}
return xmlHttpReq;
}

以上就是三种方式获取XMLHttpRequest对象的详细内容,更多请关注本站其它相关文章!

《三种方式获取XMLHttpRequest对象.doc》

下载本文的Word格式文档,以方便收藏与打印。