7

Anyone knows of a good and reliable way to find out type & version of a browser installed on client either using JavaScript/jQuery?

Looks like jQuery has some built-in functions, but it is having problems in detecting Chrome. Any other reliable way of doing it?

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
Arjun
  • 6,501
  • 10
  • 32
  • 34

3 Answers3

8

If you want information about the browser that your visitor uses, and use it for statistics or displaying information to the user, you can use the jQuery Browser Plugin.

It gives you an object in javascript that contains all of the information about the browser being used.

Be sure to do feature detection instead of browser detection when you want to determine if a certain feature is available in a browser, apply bugfixes, etc.

No need to reinvent the wheel.

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
  • looks like jQuery Browser Plugin is best suited my situation. Thanks for the info regarding feature detection. – Arjun Jun 28 '11 at 12:36
7

Approach 1:
Note: Since JQuery 1.3, jQuery.browser is deprecated

Try this :

<!DOCTYPE html>
<html>
<head>
<style>
 p { color:green; font-weight:bolder; margin:3px 0 0 10px; }
 div { color:blue; margin-left:20px; font-size:14px; }
 span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p>Browser info: (key : value)</p>

<script>
    jQuery.each(jQuery.browser, function(i, val) {
    $("<div>" + i + " : <span>" + val + "</span>")
               .appendTo( document.body );
    });</script>

</body>
</html>

Approach 2:


// A quick solution without using regexp (to speed up a little).
var userAgent = navigator.userAgent.toString().toLowerCase();
if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1)) {
alert('We should be on Safari only!');
}

Stephan
  • 41,764
  • 65
  • 238
  • 329
  • When I run this program in Chrome, following are the results:Browser info: (key : value) webkit : true version : 534.7 safari : true – Arjun Jun 28 '11 at 10:49
  • Is there any way to differentiage b/w chrome and safari? – Arjun Jun 28 '11 at 10:51
  • Not to mention that `$.browser` is deprecated since jQuery 1.3. @Arjun Is your goal to display the browser name and version to the user? – kapa Jun 28 '11 at 10:53
  • @bazmegakapa - Assume that is the case. – Arjun Jun 28 '11 at 10:57
1

Since $.browser is removed in jQuery 1.9, use this:

// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
//     ie === undefined
// If you're in IE (>=5) then you can determine which version:
//     ie === 7; // IE7
// Thus, to detect IE:
//     if (ie) {}
// And to detect the version:
//     ie === 6 // IE6
//     ie > 7 // IE8, IE9 ...
//     ie < 9 // Anything less than IE9
// ----------------------------------------------------------

// UPDATE: Now using Live NodeList idea from @jdalton

var ie = (function(){

    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');

    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );

    return v > 4 ? v : undef;

}());
Johan
  • 35,120
  • 54
  • 178
  • 293