欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

三种方式获取XMLHttpRequest对象_javascript技巧

程序员文章站 2022-05-23 12:55:01
...
获取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