最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery 1.6 Browser detection - Stack Overflow

programmeradmin7浏览0评论

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 remended language to detect browsers or should I use server-side?

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 remended language to detect browsers or should I use server-side?

Share Improve this question asked Jul 13, 2011 at 4:52 Pablo SantiagoPablo Santiago 554 bronze badges 1
  • "Best practice" for browser detection is not to do it at all. – RobG Commented Jul 13, 2011 at 5:13
Add a ment  | 

6 Answers 6

Reset to default 3

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./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./jQuery.browser/

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

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

http://api.jquery./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./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 remend 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./)

$.support or $.browser are limited.

发布评论

评论列表(0)

  1. 暂无评论