首页  >  文章  >  web前端  >  利用javascript判断浏览器类型

利用javascript判断浏览器类型

不言
不言原创
2018-07-07 17:20:151818浏览

这篇文章主要介绍了关于利用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浏览器

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

JS浏览器事件循环机制

用Node处理文件上传

以上是利用javascript判断浏览器类型的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn