Skip to content

Check Browser JavaScript | Detect user browser Example code

  • by

How to detect Safari, Chrome, IE, Firefox and Opera browser?

Use the userAgent property of the navigator object to get the User Browser in JavaScript. The userAgent will return a string, where it contains information about the browser by including certain keywords that may be tested for their presence.

Note: The information from the navigator object can often be misleading.

How to Check Browser in JavaScript Example

It is based on the navigator.userAgent and quite well tested for all browsers including iPhone, android etc.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">

       function get_browser() {
        var ua=navigator.userAgent,tem,M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; 
        if(/trident/i.test(M[1])){
            tem=/\brv[ :]+(\d+)/g.exec(ua) || []; 
            return {name:'IE',version:(tem[1]||'')};
        }   
        if(M[1]==='Chrome'){
            tem=ua.match(/\bOPR|Edge\/(\d+)/)
            if(tem!=null)   {return {name:'Opera', version:tem[1]};}
        }   
        M=M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
        if((tem=ua.match(/version\/(\d+)/i))!=null) {M.splice(1,1,tem[1]);}
        return {
          name: M[0],
          version: M[1]
      };
  }
</script>
</head>
<body>

</body>
</html>

Output:

Check Browser in JavaScript

Do comment if you need any help or questions or suggestions on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading