Home > Web Front-end > JS Tutorial > body text

How to get the browser type in Js

小云云
Release: 2018-03-19 16:21:47
Original
1606 people have browsed it

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!