Detecting browsers that are not supported by jQuery (2)

最后都变了- 提交于 2019-12-03 01:53:30

It looks like it just drops support for IE 6, 7 and 8 - you should be able to use:

HTML:

<!--[if lt IE 9]> <html data-browser="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->

JS:

if( document.documentElement.getAttribute('data-browser') !== null ){
    // not supported by jQuery 2.x
}

If you decide to support older browsers but still want the improvements the jQuery team have included in 2.x then you can use the 1.x version: http://jquery.com/download/

Update:

It's difficult/impractical to detect every browser that doesn't support jQuery, the reason is that jQuery is just a javascript library.

Different browsers and versions used to use different methods to do some basic things in javascript (like ajax), this means that some old browsers will support some features and won't support others. There isn't a straight forward this browser supports all features or this browser doesn't support any features test.

Rob W's suggestion is really good, serve the new version (2.x) to modern browsers and 1.x version (with legacy support) to old browsers.

Have a look at this article: Graceful degradation versus progressive enhancement and consider using the Modernizr library. This lets you to check which features the user's browser supports and allows you to write your applications to take full advantage of the latest advancements while still providing a good experience for users of older browsers.

A way to get the benefits of jQuery 2.0 while supporting IE 6, 7 and 8, is to use conditional comments:

<!--[if lt IE 9]><script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><![endif]-->
<!--[if IE 9]><!--><script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script><!--<![endif]-->
<script>window.jQuery||document.write('<script src="jquery.js"><\/script>');</script>
  • The first conditional comment ensures that jQuery 1.x is loaded for IE < 9.
  • The latest version of jQuery (2.0.3) is used in IE 9 and browsers where conditional comments are not recognized (IE 10 dropped support for conditional comments).
  • When jQuery fails to load from the CDN, a fallback (jquery.js, hosted on your server) is loaded.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!