阮一峰的IT笔记:首页 -> 分类 -> Javascript 查看所有文章:按分类 | 按月份

建立通用的XML HTTP请求对象

Mozilla was creating a JavaScript object called XMLHttpRequestthat behaves exactly the same as Microsoft’s version. Both Safari (as of 1.2) and Opera (as of 7.6) copied Mozilla’s implementation, creating their own XMLHttpRequestobjects.

To allow creation of an XML HTTP request in a common way, just add this simple wrapper class to your pages:

if (typeof XMLHttpRequest == “undefined” && window.ActiveXObject) {
function XMLHttpRequest() {
var arrSignatures = [“MSXML2.XMLHTTP.5.0”, “MSXML2.XMLHTTP.4.0”,
“MSXML2.XMLHTTP.3.0”, “MSXML2.XMLHTTP”,
“Microsoft.XMLHTTP”];
for (var i=0; i < arrSignatures.length; i++) {
try {
var oRequest = new ActiveXObject(arrSignatures[i]);
return oRequest;
} catch (oError) {
//ignore
}
}
throw new Error(“MSXML is not installed on your system.”);
}
}

This code allows you to use the following line to create an XML HTTP request in all browsers that support it:

var oRequest = new XMLHttpRequest();

After this point, the XML HTTP request can be used in all supporting browsers as described in the previous sections.

« 在IE中建立XML HTTP请求对象 | 首页 | 用XML HTTP 对象进行GET请求 »

About

This page contains a single entry from the blog posted on 2007年07月29日 14:43.

The previous post in this blog was 在IE中建立XML HTTP请求对象.

The next post in this blog is 用XML HTTP 对象进行GET请求.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33

Post a comment