检测浏览器类型js代码

本文详细介绍了如何通过JavaScript代码判断用户浏览器类型及版本,包括使用navigator.userAgent、addEventListener、attachEvent等方法,以及利用特定属性如ActiveXObject、appVersion来区分不同浏览器及其版本,覆盖主流浏览器如IE、Firefox、Safari、Opera等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有以下几种类型,经本人验证。

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

注释:

在 ie8 里运行 navigator.userAgent.toLowerCase() 值为:
mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/4.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0)
在 firefox v8.0.1 里运行 navigator.userAgent.toLowerCase() 值为:
mozilla/5.0 (windows nt 6.1; rv:8.0.1) gecko/20100101 firefox/8.0.1
一般浏览器发出的 navigator.userAgent值都会以 mozilla/ 开头,判断其为哪种内核的浏览器 主要看这个值里是否包含 具体内核的名称字符, 比如: MSIE  firefox Safari  Camino  Gecko

 

二、
if(typeof window.addEventListener==="function")
{
    alert("不是IE浏览器");
}
else
{
    alert("IE");
}

 

注释:

在 firefox v8.0.1 里运行 typeof window.addEventListener 值为"function"
在  ie8 里运行 typeof window.addEventListener 值为 undefined

在页面js 里, 给页面元素添加事件
IE用:element.attachEvent(”onclick”, func);。
FireFox用:element.addEventListener(”click”, func, true)。

三、

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);

 

注释:

在 firefox v8.0.1 里运行 navigator.appVersion 值为:5.0 (Windows)
在 IE8 里运行 navigator.appVersion 值为:"4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"


四、

function getBrowser()
{
   if(navigator.userAgent.indexOf("MSIE")>0) { return "MSIE"; }
    if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){ return "Firefox"; }
    if(isSafari=navigator.userAgent.indexOf("Safari")>0) { return "Safari"; }
    if(isCamino=navigator.userAgent.indexOf("Camino")>0){ return "Camino"; }
    if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){ return "Gecko"; }
}

使用时直接用if( getBrowser()=="MSIE" ){//对IE浏览器的处理}就可以。


五、

   Js 获取浏览器的IE内核版本 区别 IE6、7、8

ie 是支持js条件编译的,可以识别@cc_on 语句并作为程序执行,同时启用条件编译的

使用 @cc_on 语句激活脚本引擎中的条件编译。
语句 @_jscript_version:是js版本和ie主版本号组成的 类似 5.7 这样的数字 点号前是js版本号, 点号后是ie主版本号。

推荐在注释中使用 @cc_on.....@ 语句,比如: */
/*@cc_on alert("显示浏览器版本号:"+@_jscript_version) @*/
这样, 在不支持条件编译的浏览器中,@cc_on.....@ 语句将视为注释语句, 不会报语法错
var ie = (" " + (/*@cc_on@_jscript_version@*/ -1)).slice(-1);
 IE 6 : ie == 6
 IE 7 : ie == 7
 IE 8 : ie == 8
alert(ie);
 

六、

用document.all    !!window.ActiveXObject 判断浏览器类型 :

if (document.all){

alert('IE浏览器');

}else{

alert('非IE浏览器');

}

 
if (!!window.ActiveXObject){

alert('IE浏览器');

}else{

alert('非IE浏览器');

}

七、

区别IE6、IE7、IE8的另一方法:

    var isIE = !!window.ActiveXObject;

    var isIE6 = isIE && !window.XMLHttpRequest;

    var isIE8 = isIE && !!document.documentMode;

    var isIE7 = isIE && !isIE6 && !isIE8;

    if (isIE) {

        if (isIE6) {

            alert("ie6");

        } else if (isIE8) {

            alert("ie8");

        } else if (isIE7) {

            alert("ie7");

        }
    }

八、

其他:
使用Trident内核的浏览器:IE、Maxthon、TT、The World等
使用Gecko内核的浏览器:Netcape6及以上版本、FireFox、MozillaSuite/SeaMonkey
使用Presto内核的浏览器:Opera7及以上版本
使用Webkit内核的浏览器:Safari、Chrome。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值