51

The jQuery document tags $.browser as deprecated. So what's the replacement for it?

kapa
  • 72,859
  • 20
  • 152
  • 173
Rocky
  • 4,908
  • 6
  • 26
  • 35
  • 1
    Not really a `jQuery` method - but [css hacks](http://paulirish.com/2009/browser-specific-css-hacks/) might be useful to you. – Lix Mar 10 '12 at 10:53
  • 1
    You should avoid adding more questions to your post. You have already received numerous answers to your original question. – Lix Mar 10 '12 at 11:06
  • 1
    Thanks, I will open a new question – Rocky Mar 10 '12 at 11:06

6 Answers6

37

Based on jQuery migration plugin , I found this.

jQuery.uaMatch = function( ua ) {
    ua = ua.toLowerCase();
    var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
        /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
        /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
        /(msie) ([\w.]+)/.exec( ua ) ||
        ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) || [];
    return {
        browser: match[ 1 ] || "",
        version: match[ 2 ] || "0"
    };
};
if ( !jQuery.browser ) {
    var 
    matched = jQuery.uaMatch( navigator.userAgent ),
    browser = {};
    if ( matched.browser ) {
        browser[ matched.browser ] = true;
        browser.version = matched.version;
    }
    // Chrome is Webkit, but Webkit is also Safari.
    if ( browser.chrome ) {
        browser.webkit = true;
    } else if ( browser.webkit ) {
        browser.safari = true;
    }
    jQuery.browser = browser;
}
razzed
  • 2,607
  • 25
  • 26
takien
  • 1,000
  • 8
  • 11
  • Thanks. Some features turn out to be difficult to detect. (such as support for various values of css properties) – Gus Aug 16 '13 at 16:17
  • While the main answer is correct, if you are working on a site which has legacy code which depends on `$.browser`, this answer is the way to go, unfortunately, until the old code can be excised. – razzed Sep 22 '14 at 15:06
  • 4
    Thank you for actually answering the question. – Ryan Ore Jan 29 '15 at 15:21
  • 2
    Yep, this definitely works as a patch for old plugins. (and answers the question) – Miro Feb 26 '15 at 19:00
  • 1
    This is helpful in a way the jquery docs quoted above is not. – davidjmcclelland Jul 14 '17 at 15:33
24

If you really need good old $.browser

According to the docs, this feature was deprecated in 1.3, and totally removed in 1.9, although it is still available in the official jQuery Migrate plugin.

If you want to do it right

Depending on browser detection is not a good idea. Feature detection is the way to go (Modernizr is a great tool for that). jQuery had a $.support() method to provide some feature detection, but it is now deprecated as well. They also suggest using Modernizer.

If you really need browser detection

Fixing browser quirks is not a valid use case for browser detection, but there are other use cases. Use any Javascript browser detection tool (like bowser), as this functionality does not depend on jQuery at all.

Community
  • 1
  • 1
kapa
  • 72,859
  • 20
  • 152
  • 173
  • 1
    [Modernizr](http://www.modernizr.com/) is a great tool for CSS3/HTML5 feature detection. – Marcelo Mason Mar 10 '12 at 12:20
  • @phaed Thanks, added it to the answer. – kapa Mar 10 '12 at 12:21
  • 6
    You're wrong @bažmegakapa, there are browser bugs, like `window.onload event doesn't fire in chrome`, so we need to bridge those with browser detection. – Lajos Mészáros Jun 14 '13 at 08:49
  • @MészárosLajos I am not aware of the browser bug you're mentioning, never had a problem with it. Even if such a bug exists, it does not invalidate what I'm saying, as there is an exception to every rule. In 95% of all cases feature detection is the way to go, browser detection will only introduce further problems. – kapa Jun 14 '13 at 09:02
  • 1
    True, @bažmegakapa, I was just saying that don't say never in programming. http://stackoverflow.com/questions/7493293/window-load-chrome-safari-problem – Lajos Mészáros Jun 17 '13 at 08:45
  • 1
    I'm building an app for reporting bugs, which gathers data about the user's browser/device. So It's completely reasonable to want this functionality. Device detection isn't evil, just a matter of why you're using it. Thanks @takien for the quick answer. – Ryan Ore Jan 29 '15 at 15:26
  • @RyanOre Where did I say you should not detect the browser even if that's the data you actually need to collect? That would actually be an insane statement, right? :) – kapa Jan 29 '15 at 17:41
  • Indeed, that wasn't really directed at you. Moreso at the overzealous reaction people have when the topic comes up. You gave a fine answer. But OP didn't say why he needed it, just that he did. – Ryan Ore Jan 29 '15 at 20:34
  • @RyanOre He did not say why, yes. But whatever the reason, this feature does not have a place in a general DOM (and AJAX) library like jQuery, unless it is meant to be used for fixing up browser differences. The use case you describe is very special, so for only that this feature should not be the part of jQuery. – kapa Jan 29 '15 at 22:23
  • @kapa Navigator is the replacement but it is not recommend , please see this page for more details http://www.w3schools.com/jsref/prop_nav_appcodename.asp and this page too http://www.javascriptkit.com/javatutors/navigator.shtml – Karim Samir Mar 23 '15 at 14:48
  • @KarimSamir `navigator` has nothing to do with jQuery. – kapa Mar 23 '15 at 15:04
  • @kapa yes I know , JQuery doesn't support browser detection any more , you can check here for more details https://learn.jquery.com/code-organization/feature-browser-detection/ – Karim Samir Mar 23 '15 at 15:51
  • 1
    @kapa people are still stuck updating legacy code fully of $.browser and coming to this question years later. Your answer provides some information available in the docs, not a way to work the real problem when your codebase is frozen in amber – davidjmcclelland Jul 14 '17 at 15:32
  • @davidjmcclelland Thanks for your comment, I updated my answer to be more helpful, without losing the point I was trying to make. But maybe if your codebase is so frozen, you could stay with old jQuery? – kapa Jul 15 '17 at 10:01
3

There isn't a direct replacement. You should be using feature detection rather than browser detection (do you have a good reason to need to know the browser?), so you can use the $.support property. (It says as much in the API doco for $.browser.)

nnnnnn
  • 138,378
  • 23
  • 180
  • 229
  • 1
    Jquery cautions against using $.support: "A collection of properties that represent the presence of different browser features or bugs. Intended for jQuery's internal use; specific properties may be removed when they are no longer needed internally to improve page startup performance. For your own project's feature-detection needs, we strongly recommend the use of an external library such as Modernizr instead of dependency on properties in jQuery.support." – Chris Middleton Aug 26 '14 at 17:11
2

jQuery.support

Emil Vikström
  • 84,510
  • 15
  • 132
  • 168
1

The jquery-browser-plugin is a good drop in replacement

Claus
  • 1,394
  • 10
  • 17
1

you can use navigator variable from javascript

console.log(navigator)

but if you want to check the compatiblility with a function of jquery you can use the support var like

$.support.ajax
Spidfire
  • 5,233
  • 5
  • 26
  • 36