Home > Web Front-end > JS Tutorial > Use javascript to determine browser type

Use javascript to determine browser type

不言
Release: 2018-07-07 17:20:15
Original
1880 people have browsed it

This article mainly introduces the use of javascript to determine the browser type, which has certain reference value. Now I share it with you. Friends in need can refer to it

Related methods to determine the browsing type

Console prints browser-related information

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

The judgment is IE, Firefox, and Chrome browsers

       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()
Copy after login

The above code can judge IE, Firefox, and Google Chrome, but domestic QQ browsing When Sogou Browser is running, the alert result is "Chrome Browser"

In judging whether it is QQ or Chrome Browser

 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()
Copy after login

360 Browser Weird

360 Browse The browser cannot detect that it is a 360 browser through the above method

//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()
Copy after login

In this way, it can be determined that it is a 360 browser

The above is the entire content of this article, I hope it will help everyone learn Helpful, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

JS browser event loop mechanism

Use Node to process file uploads

The above is the detailed content of Use javascript to determine browser type. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template