现在的位置: 首页 > 综合 > 正文

如何检查浏览器类型

2013年09月01日 ⁄ 综合 ⁄ 共 1075字 ⁄ 字号 评论关闭

转到http://www.51obj.cn/?p=368

之前一直使用检查navigator.userAgent字符串来判断浏览器类型

switch(navigator.userAgent.toLowerCase().indexOf("msie"))//firefox|opera|safari|msie
{
    case(-1):
        alert("DOM浏览器");
    default:
        alert("IE浏览器");
}
f  

今天在看一篇文章中提到用户在某些浏览器可以更改userAgent,这样就不能单纯使用这个方法来检查浏览器。在实际使用中一般检查最多的是ie浏览器与标准dom浏览器的区别,这样可以使用window.addEventListener来判断这两种类型的浏览器

if(typeof window.addEventListener==="function"){
    alert("DOM浏览器");
}else{
    alert("IE");
}

高手写的一个检测浏览器的代码

var isIE = !!(window.attachEvent && !window.opera);
var isOpera = !!window.opera;
var isSafari = navigator.userAgent.indexOf(’AppleWebKit/’) > -1;
var isMoz = navigator.userAgent.indexOf(’Gecko’) > -1 && navigator.userAgent.indexOf(’KHTML’) == -1;
var isMobileSafari = !!navigator.userAgent.match(/Apple.*Mobile.*Safari/);

var isIE5 = (navigator.appVersion.indexOf("MSIE 5")>0) || (navigator.appVersion.indexOf("MSIE")>0 && parseInt(navigator.appVersion)> 4);
var isIE55 = (navigator.appVersion.indexOf("MSIE 5.5")>0);
var isIE6 = (navigator.appVersion.indexOf("MSIE 6")>0);
var isIE7 = (navigator.appVersion.indexOf("MSIE 7")>0);

var isIE8 = (navigator.appVersion.indexOf("MSIE 8")>0);

抱歉!评论已关闭.