Detecting browsers that are not supported by jQuery (2)

后端 未结 2 834
星月不相逢
星月不相逢 2021-02-05 11:50

Forgive me if there is an obvious answer to this question, but I haven\'t been able to find it. I am considering switching over to jQuery 2 and, although I\'m not concerned abou

相关标签:
2条回答
  • 2021-02-05 12:10

    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.
    0 讨论(0)
  • 2021-02-05 12:22

    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.

    0 讨论(0)
提交回复
热议问题