三种方式获取XMLHttpRequest对象

前端技术 2023/09/05 JavaScript
获取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;
}

本文地址:https://www.stayed.cn/item/14562

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。