利用javascript判断浏览器类型

判断浏览类型的相关方法

控制台打印浏览器相关信息

window.navigator.userAgent.toLowerCase()//将浏览器信息获取,并转成小写

判断是ie、火狐、chrome浏览器

       function isBrowser(){
        var agent=navigator.userAgent.toLowerCase()
        console.log(agent)
            if(agent.indexOf('chrome')>0){
                alert("chrome浏览器")
            }
            if(agent.indexOf('firefox')>0){
                alert("firefox浏览器")
            }
            if(agent.indexOf('trident')>0){
                alert("IE浏览器")
            }
       }
    isBrowser() 

上面代码可以判断ie,火狐,谷歌浏览器,但是 国内的QQ浏览器,搜狗浏览器运行的时候alert的结果是”Chrome浏览器”

在判断是qq还是Chrome浏览器

 function isBrowser(){
        var agent=navigator.userAgent.toLowerCase()
        console.log(agent)
           System=function(){
            if(agent.indexOf('qqbrowser')>0){//判断是qq浏览器还是其它浏览器
               return alert("qq浏览器")
            }
            if(agent.indexOf("se 2.x")>0){
                return alert("搜狗浏览器")
            }
            alert('chrome浏览器')
           }
           System()
            if(agent.indexOf('firefox')>0){
                alert("firefox浏览器")
            }
            if(agent.indexOf('trident')>0){
                alert("IE浏览器")
            }
       }
    isBrowser() 

360浏览器奇葩

360浏览器通过上面的方法并不能检测出是360浏览器

//application/vnd.chromium.remoting-viewer 可能为360特有 通过_mine判断是否是360
function isBrowser(){
        var agent=navigator.userAgent.toLowerCase()
        console.log(agent)
           System=function(){
            if(agent.indexOf('qqbrowser')>0){//判断是qq浏览器还是其它浏览器
               return alert("qq浏览器")
            }
            if(agent.indexOf("se 2.x")>0){
                return alert("搜狗浏览器")
            }
             var is360 = _mime("type", "application/vnd.chromium.remoting-viewer");
            
            if (is360) { 
                return "360浏览器"
            }
      
        //检测是否是谷歌内核(可排除360及谷歌以外的浏览器)
        //测试mime
        function _mime(option, value) {
            var mimeTypes = navigator.mimeTypes;
            console.log(mimeTypes)
            for (var mt in mimeTypes) {
                if (mimeTypes[mt][option] == value) {
                    return true;
                }
            }
            return false;
        }
            alert('chrome浏览器')
           }
           System()
            if(agent.indexOf('firefox')>0){
                alert("firefox浏览器")
            }
            if(agent.indexOf('trident')>0){
                alert("IE浏览器")
            }
       }
    isBrowser() 

这样就可以判断出是360浏览器
是通过两位作者1作者2的方法总结出来的。

    原文作者:随她
    原文地址: https://segmentfault.com/a/1190000015553755
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞