0

So I know $.browser has been deprecated and "frowned upon", since jQuery 1.3, but it continues to exist & work in the code.

It's still using the plain javascript: navigator.userAgent to determine the browser being used, as well as the version.


Now is there something about these I don't know about navigator itself, that I shouldn't be using either $.browser or plain vanilla JS to get the browser/version? I just want to make sure when they have IE8 (for example), they really do have it, and I'm not processing the wrong code.


What other alternatives do we have for browser sniffing? I know about $.support, I use modernizr, but sometimes I need just need the down and dirty browser version, instead of seeing what the browser is capable of handling (I think that is a completely different problem solver).

Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96
  • 1
    May be http://www.quirksmode.org/js/detect.html to know what browser it is... but ultimately to detect the support -> **http://www.quirksmode.org/js/support.html** – Selvakumar Arumugam Sep 11 '12 at 15:12
  • 4
    Generally it's recommended not to try to guess what the browser is but to check if a function is available. There are too many browsers and variants... – Denys Séguret Sep 11 '12 at 15:12
  • The recommend way is to use feature detection, not browser version detection. – Darin Dimitrov Sep 11 '12 at 15:13
  • jQuery internally doesn't use it, so it ends up being extra code that a very small number of people still use. It makes more sense to remove it than to keep it there. If you still wish to use it, you'll be able to use the "compatability plugin" that the jQuery team will be releasing with 1.9 and 2.0 to get that method back. – Kevin B Sep 11 '12 at 15:13
  • @dystroy - "check if a function is available", what do you mean by that? – Mark Pieszak - Trilon.io Sep 11 '12 at 15:19

3 Answers3

1

You kind of answer the question yourself. The ideal is to check for feature support. As more browsers and devices come onto the market this approach should scale.

However if you want to do something 'down and dirty' then browser detection of course works, but only so far as you will know your code works in the existing set of browsers (or even just those you've tested your code with).

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
1

Generally it's recommended not to try to guess what the browser is but to check if a function is available. There are too many browsers and variants.

To check if a function is available, you simply do this :

if (!Array.prototype.map) {
    // not available, shut down computer !
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

If a "must" to know which browser on the page for me, I use this personally;

(function() {
    var re_browsers = {
        firefox: /firefox\/([\d\.]+)/,
        chrome: /chrome\/([\d\.]+)/,
        safari: /webkit.*?version\/([\d\.]+)/,
        opera: /opera.*?version\/([\d\.]+)/,
        ie: /msie\s+([\d\.]+)/
        // ...
    };

    var ua = window.navigator.userAgent.toLowerCase(), k, re, browser = {};
    for (k in re_browsers) {
        if (re = re_browsers[k].exec(ua)) {
            break;
        }
    }
    browser[k] = true;
    browser["version"] = parseFloat(re && re[1]);
    browser["versionOrig"] = re[1];

    jQuery.extend({browser: browser});
})();
Kerem
  • 11,377
  • 5
  • 59
  • 58