2

Possible Duplicate:
Why is $.browser deprecated - and what is a better alternative?

I have historically used the $.browser property in my jQuery to determine the browser. I found it tremendously helpful and useful.

I also have historically linked to the latest version of jQuery. Like this:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

Well, apparently jQuery 1.9 is out not and the support for this property is REMOVED.

Does anyone know a good jQuery plugin to use as an alternative?

I do know that the recommended method is to use $.support, and I will use that sometimes.

But sometimes it's just easier to know what browser someone is using.

Can anyone help?

Community
  • 1
  • 1
Steve C.
  • 577
  • 1
  • 6
  • 19
  • 5
    jQuery says to use Modernizr ("We recommend using feature detection with a library such as Modernizr.") – j08691 Jan 16 '13 at 21:41
  • 4
    I suggest against linking to the latest version in that way, you get less cacheing. A better link would be `http://code.jquery.com/jquery-1.8.3.min.js` and then modifying it when you want to move to a new version of jQuery. You can add $.browser and other depreciated methods back into jQuery by using the jQuery Migrate plugin. – Kevin B Jan 16 '13 at 21:42
  • 2
    If you want to know what browser someone is using, inspect the navigator object. – Kevin B Jan 16 '13 at 21:43
  • Kevin: Thanks, too, for your thoughts. I may go one of the routes you suggested. – Steve C. Jan 16 '13 at 21:54
  • why is it easier to know what browser is being used? A lot easier to use feature detection – charlietfl Jan 16 '13 at 22:11
  • charlietfl: Because sometimes I'm not testing for a feature. For one example, sometimes a specific browser (usually Microsoft) just renders a piece of CSS slightly differently or off a pixel or two from where I want it. So what am I testing for support for? It supports the CSS, but the value needs to be tweaked. If I can isolate browsers, it makes it easy. – Steve C. Jan 17 '13 at 15:03

2 Answers2

1

The browser object simply parsed the navigator.userAgent for you. You could always parse that yourself if you are so inclined.

Nothing stops you from plucking the code from an older JQuery version and then putting that into your own jquery plugin.

The Jquery docs recommend against the browser object and even state that it is vulnerable to spoofing:

Because $.browser uses navigator.userAgent to determine the platform, it is vulnerable to spoofing by the user or misrepresentation by the browser itself. It is always best to avoid browser-specific code entirely where possible. The $.support property is available for detection of support for particular features rather than relying on $.browser.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • Thanks, John. Yeah, I thought of that, too. I loved the simplicity of $.browser, but I suppose it's not too bad to just go with straight javascript there. I also read about the vulnerability to misrepresentation, which I'm sure is a reason that they see fit to discontinue it. I'm curious to see if anyone else has other alternatives. But I appreciate your input, and I'll check back in. – Steve C. Jan 16 '13 at 21:52
0

You could always go to the source.

This is not my code, this is adapted from jQuery source code

DEMO: http://jsfiddle.net/KKwqU/

js:

function ExtendJqueryBrowser(){

 jQuery.extend({browser:{}});

 function uaMatch(ua){
     ua = ua.toLowerCase();
     var rwebkit = /(webkit)[ \/]([\w.]+)/,
     ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
     rmsie = /(msie) ([\w.]+)/, 
     rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/;
     var match = rwebkit.exec( ua ) || ropera.exec( ua ) || rmsie.exec( ua ) || ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) || [];
     return { browser: match[1] || "", version: match[2] || "0" };
 }

    var browserMatch = uaMatch( navigator.userAgent );
    if ( browserMatch.browser ) {
     jQuery.browser[ browserMatch.browser ] = true;
     jQuery.browser.version = browserMatch.version;
    }

    // Deprecated, use jQuery.browser.webkit instead
    if ( jQuery.browser.webkit ) {
     jQuery.browser.safari = true;
    }
}
Travis J
  • 81,153
  • 41
  • 202
  • 273