JavaScript Browser Detection

Here you will learn how to identify which browser you are on using JavaScript or which browser your web page is running on.

JavaScript Browser Detection Example

Here is an simple example of browser detection using JavaScript:

<html>
<head>
<title>JavaScript Browser Detection</title>
</head>
<body>

<h3>JavaScript Browser Detection Example</h3>
   <script type="text/javascript">
   var userAgent = navigator.userAgent;
   var opera_browser = (userAgent.indexOf('Opera') != -1);
   var ie_browser = (userAgent.indexOf('MSIE') != -1);
   var mozilla_browser = (userAgent.indexOf('Gecko') != -1);
   var netscape_browser = (userAgent.indexOf('Mozilla') != -1);
   var browser_version = navigator.appVersion;
   if(opera_browser)
   {
      document.write("<p>It is Opera based browser.</p>");
   }
   else if(mozilla_browser)
   {
      document.write("<p>It is Mozilla based browser.</p>");
   }
   else if(ie_browser)
   {
      document.write("<p>It is IE based browser.</p>");
   }
   else if(netscape_browser)
   {
      document.write("<p>It is Netscape based browser.</p>");
   }
   else
   {
      document.write("<p>It is an Unknown browser.</p>");
   }
   document.write("Browser Version Information: " + browser_version);
   </script>
   
</body>
</html>

Here is the sample output of the above JavaScript browser detection example code. This output is taken from Mozilla Firefox browser:

javascript browser detection

It will display the following result for your browser :

JavaScript Online Test


« Previous Tutorial Next Tutorial »