方法一:

可以很好的捕获异常,故推荐

[CODE=javascript]
function XHR(){ //创建XMLHttpRequest对象
var xhr;
try{
xhr=new XMLHttpRequest();
}catch(e){
var a=[‘MSXML2.XMLHTTP.5.0′,’MSXML2.XMLHTTP.4.0′,’MSXML2.XMLHTTP.3.0′,’MSXML2.XMLHTTP’,’MICROSOFT.XMLHTTP.1.0′,’MICROSOFT.XMLHTTP.1′,’MICROSOFT.XMLHTTP’];
for (var i=0;i try{
xhr = new ActiveXObject(a[i]);
break;
}catch(e){}
}
}
if (!xhr) alert(“Error creating the XMLHttpRequest object.”);
else return xhr;
}
[/CODE]

方法二:

简单,一句话搞定

[CODE=javascript]
var xml = window.ActiveXObject ? new ActiveXObject(“Microsoft.XMLHTTP”) : new XMLHttpRequest();
[/CODE]