Home  >  Article  >  Web Front-end  >  How to get the browser type in Js

How to get the browser type in Js

小云云
小云云Original
2018-03-19 16:21:471653browse

This article mainly shares with you how to obtain the browser type in Js. It is mainly shared with you in the form of code. I hope it can help you.

<script>  
    function myBrowser(){  
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
        var isOpera = userAgent.indexOf("Opera") > -1;  
        //判断是否Opera浏览器  
        if (isOpera) {  
            return "Opera"  
        };  
        //判断是否Firefox浏览器  
        if (userAgent.indexOf("Firefox") > -1) {  
            return "FF";  
        }  
        //判断是否Chrome浏览器  
        if (userAgent.indexOf("Chrome") > -1){  
            return "Chrome";  
        }  
        //判断是否Safari浏览器  
        if (userAgent.indexOf("Safari") > -1) {  
            return "Safari";  
        }  
        //判断是否IE浏览器  
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {  
            return "IE";  
        };  
    }  
  
    //以下是调用上面的函数  
    var mb = myBrowser();  
    if ("IE" == mb) {  
        alert("我是 IE");  
    }  
    if ("FF" == mb) {  
        alert("我是 Firefox");  
    }  
    if ("Chrome" == mb) {  
        alert("我是 Chrome");  
    }  
    if ("Opera" == mb) {  
        alert("我是 Opera");  
    }  
    if ("Safari" == mb) {  
        alert("我是 Safari");  
    }  
</script>

Related recommendations:

jQuery method of obtaining the browser type and version number

js obtains the browser and screen Various widths and heights

Simple JS to get the browser IP code

The above is the detailed content of How to get the browser type in Js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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