Pablo Santiago
Pablo Santiago

Reputation: 55

jQuery 1.6 Browser detection

With all these api changes in jquery, what is the 'latest' best practice to detect browser name and version in jQuery?

Also, is jquery the recommended language to detect browsers or should I use server-side?

Upvotes: 3

Views: 1871

Answers (6)

Bamboo
Bamboo

Reputation: 2056

HTML5, CSS needs, use Modernizr (http://modernizr.com/)

$.support or $.browser are limited.

Upvotes: 0

run
run

Reputation: 1186

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

you can refer refer

Upvotes: 0

Kevin Jhangiani
Kevin Jhangiani

Reputation: 1607

The 'best practice' to detect a browser using jQuery is with jQuery.support:

http://api.jquery.com/jQuery.support/

However if for whatever reason you actually need to know the specific browser, rather than whether a feature is supported, jquery does have a browser detection using the user-agent:

http://api.jquery.com/jQuery.browser/

As mentioned on that page however, this is not recommended and may be moved to a plugin in a future version of jQuery.

Upvotes: 3

CuriousMind
CuriousMind

Reputation: 34165

You want to avoid browser detection & instead do feature detection.browser detection can be easily faked & therefore is unreliable. see a nice post by John Resig on this subject.

for more details, Google for "Browser detection versus feature detection"

Upvotes: 3

Jacob Mouka
Jacob Mouka

Reputation: 2095

jQuery.browser works well. in most cases all methods will look up the browser agent text, which can be changed by the user, so there is no sure-fire method.

they recommend not using this and to check for the presence of specific APIs, but if you need to know the browser this method works well.

Upvotes: 1

Evan
Evan

Reputation: 7416

$.browser is easy by not as recommended any more.

http://api.jquery.com/jQuery.browser/

Remember, it's trivial to fake your user agent and I don't believe it's possible to any more reliably detect browsers server-side. Just don't use detection for anything super critical.

jQuery instead suggests you detect for specifically the reasons why you're checking for browsers in the first place.

For example if (jQuery.support.boxModel) ....

http://api.jquery.com/jQuery.support/

Upvotes: 2

Related Questions