您的位置:首页 > 其它

判断IE各版本浏览器的最直接有效的方式

2017-08-15 11:12 232 查看
首先,判断是否是IE浏览器,有如下两种方式:
第一种,通过userAgent信息来判断


[javascript] view
plain copy

 print?

if(navigator.userAgent.indexOf("MSIE")>0){  

    //是IE浏览器  

}  

第二种:通过ActiveXObject这个IE浏览器专有对象来判断


[javascript] view
plain copy

 print?

if(!!window.ActiveXObject){  

    //是IE浏览器  

}  

然后,判断在IE浏览器运行环境中的IE版本(6,7,8,9,10等)

在这之前我们先来了解两个对象:
第一个对象:document.documentMode这个东西是IE渲染文档模式;IE6和IE7不识别,控制台会输出undefined,IE8及以上才支持识别,控制台会输出对应版本:8,9,10,11。
第二个对象:window.XMLHttpRequest,在IE7及以上才支持。
我们会用到这两个东西的,闲话不多说,直接上表达式:
[javascript] view plain copy print?//IE6判断:  
var isIE6 = !!window.ActiveXObject && !window.XMLHttpRequest;  
//或者:  
if(navigator.userAgent.indexOf("MSIE 6.0")>0){  
    alert("ie6");  
}  
//IE7判断:  
if(navigator.userAgent.indexOf("MSIE 7.0")>0){  
    alert("ie7");  
}  
  
//IE8判断:  
if(!!window.ActiveXObject && document.documentMode==8){  
    alert("ie8");  
}  
  
//IE9判断:  
if(!!window.ActiveXObject && document.documentMode==9){  
    alert("ie8");  
}  
  
//IE10判断:  
if(!!window.ActiveXObject && document.documentMode==10){  
    alert("ie8");  
}  

通过以上判断,您也可以组合延伸出其他的一些用法:
[javascript] view plain copy print?if(!!window.ActiveXObject && !document.documentMode){  
    alert("ie版本小于8");  
}  
  
if(!!window.ActiveXObject && (!document.documentMode||document.documentMode<9)){  
    alert("ie版本小于9");  
}  
  
if(!!window.ActiveXObject && (!document.documentMode||document.documentMode<10)){  
    alert("ie版本小于10");  
}  
  
if(!!window.ActiveXObject && !!document.documentMode){  
    alert("ie版本大于等于8");  
}  
  
if(!!window.ActiveXObject && document.documentMode>8){  
    alert("ie版本大于8");  
}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: