简体   繁体   中英

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?

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 "

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.

$.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/

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.

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

$.support or $.browser are limited.

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

you can refer refer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM