首頁  >  文章  >  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,火狐,Google瀏覽器,但國內的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瀏覽器事件循環機制


####################################################################### ###

以上是利用javascript判斷瀏覽器類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn