由于微软最近有推出了IE8.0,和不久推出的7.0,而现在市面上的书籍几乎没有考虑到7.0和8.0的情况。而7.0和8.0中的xmlhttprequest对象又和旧版本的不一样。它们两个已经对xmlhttprequest对象开始支持了。
eg:
var xmlHttp = null;
if (window.XMLHttpRequest) {
// If IE7, Mozilla, Safari, and so on: Use native object
xmlHttp = new XMLHttpRequest();
}
else
{
if (window.ActiveXObject) {
// ...otherwise, use the ActiveX control for IE5.x and IE6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
虽然这样是可以在IE7.0中建立xmlhttprequest对象的,但是ie7.0中的xmlhttprequest对象并不支持对本地资源的访问。所以在这样做还是有缺陷的……
所以可以考虑这样:
if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //ie的新版本
}
else if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
}
else if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}