-1

Possible Duplicate:
Best way to find Browser type & version?

In HTML/JavaScript, How can I identify which browser the user is using, and by that choose the code, I mean: if the browser is crhome do that... else ... Thanks

Community
  • 1
  • 1

2 Answers2

5

You can do it using navigator and get all the details. Just make a google search!

<div id="example"></div>

<script type="text/javascript">

txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";

document.getElementById("example").innerHTML=txt;

</script>

Hope this helps! :)

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
4

It's not recommended to detect browsers since browsers are a moving targets. For example, today Chrome doesn't support feature X but several months later it starts supporting it. If you simply put

// Here I detect Chrome browser without a third-party library
if (navigator.userAgent.toLowerCase().indexOf('chrome') === -1) {
    doFeatureX()
else {
    console.log('Sorry, no Feature X for you.')
}

then you lock Chrome users out forever. You should do feature detection instead and there are quite a few libraries to help you with that:

if (Modernizr.geolocation) {
    // do stuff with user's coordinates
}

But of course you can detect browsers if you want to. For example, you'd like to encourage people to upgrade their browser:

// Here I'm using jQuery
if ($.browser.msie && parseInt($.browser.version, 10) < 9) {
    // show link to http://microsoft.com/ie
} else if ($.browser.mozilla && parseInt($.browser.version, 10) < 11) {
    // show link to http://mozilla.com/firefox
} // etc.