Most common browser user agents. Detecting browser with JavaScript

March 2nd, 2010 by Alex Leave a reply »

Ok, here is the very simple function to detect the client browser with JavaScript.
It works on some basic browsers IE, Firefox, Chrome, Safari, Opera, Maxthon and some rare Linux browsers Namoroka and BonEcho.
Plus, it detects whether the client uses Windows, Mac or Linux.

Of course, this is a very simple function, and it may have some bugs or inaccurate checks. Please comment if you have problems using it.

The usage is very simple. Just include the script within your page and run an instance of the detector.

<script type='text/javascript' src='browserdetector.js'></script>
<script>
var bd = new BrowserDetector();
</script>

The object is set up and ready to use. It already parsed the user agents and all the variables you need are set.
Here are some basic functions you can use and try out:

document.writeln('<div>You are using '+bd.browserString()+'</div>');
document.writeln('<div>On '+bd.OSString()+'</div>');
document.writeln('<div>'+bd.browserName()+'</div>');
document.writeln('<div>'+bd.browserVersion()+'</div>');
document.writeln('<div>'+bd.OSName()+'</div>');
document.writeln('<div>'+bd.OSVersion()+'</div>');
 
if (bd.isIE())
  document.writeln('You are using Internet Explorer!');
if (bd.isFF())
  document.writeln('You are using Firefox!');
if (bd.isChrome())
  document.writeln('You are using Chrome!');
if (bd.isSafari())
  document.writeln('You are using Safari!');
if (bd.isOpera())
  document.writeln('You are using Opera!');
if (bd.isMaxthon())
  document.writeln('You are using Maxthon!');

The examples files are here
Download the JavaScript here

This page can be found by searching for:

common user agents



2 comments

  1. Denilson says:

    Although I appreciate your efforts with this script, I must warn that Browser Sniffing (or browser detection) is generally a BAD IDEA.

    There are many posts everywhere explaining why it’s a bad practice. Here are two of them:

    http://www.sitepoint.com/blogs/2009/05/31/why-browser-sniffing-stinks/

    http://www.quirksmode.org/blog/archives/2006/08/the_dangers_of.html

    Even if the author understands the dangers of this practice, I’m leaving this comment here for future readers.

  2. Alex says:

    I totally agree with you. This method here may only be used for some testing purposes, or small websites (because you probably saw i use useragent string here which can be easily replaced).
    But the reason to use this is mainly because regular users usually don’t know anything about user agents, and thus this method can be used sometimes.
    But, just in case you’ll already in it, the best way to detect browsers is using the “personal methods”, like window.external in IE or window.sidebar for Firefox etc