方法一:推薦
// 創建一個布爾型變量
var xmlhttp = false;
// 檢查使用的是否為IE瀏覽器
try{
// 如果JS的版本大于5
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
alert("You are using Microsoft Internet Explorer.");
}catch(e){
// 如果不是,則使用老版本的ActiveX對象
try{
// 如果使用的是IE瀏覽器
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
alert("You are using Microsoft Internet Explorer.");
}catch(e){
// 使用非IE瀏覽器
xmlhttp = false;
}
}
// 如果使用的是IE非瀏覽器
if(!xmlhttp && typeof XMLHttPRequest != 'undefined'){
xmlhttp = new XMLHttpRequest();
alert("You are not using Microsoft Internet Explorer.");
}
方法二:
var xmlhttp;
// 如果ActiveX對象可用,則使用的肯定是IE瀏覽器
(!window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}else{
// 使用javascript方法處理
xmlhttp = new XMLHttpRequest();
}
新聞熱點
疑難解答