How do I detect IE 8 with jQuery?

前端 未结 12 1370
一生所求
一生所求 2020-11-30 19:14

I need to detect not only the browser type but version as well using jQuery. Mostly I need to find out if it is IE 8 or not.

I am not sure if I am doing it correctly

相关标签:
12条回答
  • 2020-11-30 19:39

    Note:

    1) $.browser appears to be dropped in jQuery 1.9+ (as noted by Mandeep Jain). It is recommended to use .support instead.

    2) $.browser.version can return "7" in IE >7 when the browser is in "compatibility" mode.

    3) As of IE 10, conditional comments will no longer work.

    4) jQuery 2.0+ will drop support for IE 6/7/8

    5) document.documentMode appears to be defined only in Internet Explorer 8+ browsers. The value returned will tell you in what "compatibility" mode Internet Explorer is running. Still not a good solution though.

    I tried numerous .support() options, but it appears that when an IE browser (9+) is in compatibility mode, it will simply behave like IE 7 ... :(

    So far I only found this to work (kind-a):

    (if documentMode is not defined and htmlSerialize and opacity are not supported, then you're very likely looking at IE <8 ...)

    if(!document.documentMode && !$.support.htmlSerialize && !$.support.opacity) 
    {
        // IE 6/7 code
    }
    
    0 讨论(0)
  • 2020-11-30 19:42

    Here is the Jquery browser detect plugin to identify browser/os detection.

    You can use this for styling purpose after including the plugin.

    $("html").addClass($.os.name);
    $("body").addClass($.browser.className);
    $("body").addClass($.browser.name);
    
    0 讨论(0)
  • 2020-11-30 19:43

    Assuming...

    • ...that it's the crunky rendering engine of old versions of IE you're interested in detecting, to make a style look right in old IE (otherwise, use feature detection)
    • ...that you can't just add conditional comments to the HTML - e.g. for JS plugins that can be applied to any page (otherwise, just do the trick of conditional classes on <body> or <html>)

    ...then this is probably the best trick (based on this non-jQuery, slightly less flexible variant). It creates then tests for then removes an appropriate conditional comment.

    (Conditional comments are ignored in IE10+ 'standards mode' - but that should be fine since IE10+ 'standards mode' doesn't have a crazy rendering engine!)

    Drop in this function:

    function isIE( version, comparison ){
        var $div = $('<div style="display:none;"/>');
    
        // Don't chain these, in IE8 chaining stops some versions of jQuery writing the conditional comment properly
        $div.appendTo($('body'));
        $div.html('<!--[if '+(comparison||'')+' IE '+(version||'')+']><a>&nbsp;</a><![endif]-->');
    
        var ieTest = $div.find('a').length;
        $div.remove();
        return ieTest;
    }
    

    Then use it like this:

    if(isIE()){ /* runs in all versions of IE after 4 before standards-mode 10 */ }
    
    if(isIE(8)){ /* runs in IE8 */ }
    
    if(isIE(9)){ /* runs in IE9 */ }
    
    if(isIE(8,'lte')){ /* runs in IE8 or below */ }
    
    if(isIE(6,'lte')){ /* if you need this, I pity you... */ }
    

    I'd also suggest caching the results of this function so you don't have to repeat it. For example, you could use the string (comparison||'')+' IE '+(version||'') as a key to store and check for the result of this test in an object somewhere.

    0 讨论(0)
  • 2020-11-30 19:44

    You should also look at jQuery.support. Feature detection is a lot more reliable than browser detection for coding your functionality (unless you are just trying to log browser versions).

    0 讨论(0)
  • 2020-11-30 19:46

    It is documented in jQuery API Documentation. Check for Internet Explorer with $.browser.msie and then check its version with $.browser.version.

    UPDATE: $.browser removed in jQuery 1.9

    The jQuery.browser() method has been deprecated since jQuery 1.3 and is removed in 1.9. If needed, it is available as part of the jQuery Migrate plugin. We recommend using feature detection with a library such as Modernizr.

    0 讨论(0)
  • 2020-11-30 19:50

    document.documentMode is undefined if the browser is not IE8,

    it returns 8 for standards mode and 7 for 'compatable to IE7'

    If it is running as IE7 there are a lot of css and dom features that won't be supported.

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