开发者

jQuery 1.6 Browser detection

With all these api changes in jquery, what is the 'latest' best practice t开发者_JS百科o 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.


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

you can refer refer


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

$.support or $.browser are limited.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜