判斷瀏覽類型的相關方法html
window.navigator.userAgent.toLowerCase()//將瀏覽器信息獲取,並轉成小寫
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瀏覽器"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瀏覽器瀏覽器
//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()