Detect IE version (prior to v9) in JavaScript

前端 未结 30 1438
半阙折子戏
半阙折子戏 2020-11-22 08:44

I want to bounce users of our web site to an error page if they\'re using a version of Internet Explorer prior to v9. It\'s just not worth our time and money to

相关标签:
30条回答
  • 2020-11-22 08:58

    Detect IE in JS using conditional comments

    // ----------------------------------------------------------
    // A short snippet for detecting versions of IE in JavaScript
    // without resorting to user-agent sniffing
    // ----------------------------------------------------------
    // If you're not in IE (or IE version is less than 5) then:
    //     ie === undefined
    // If you're in IE (>=5) then you can determine which version:
    //     ie === 7; // IE7
    // Thus, to detect IE:
    //     if (ie) {}
    // And to detect the version:
    //     ie === 6 // IE6
    //     ie > 7 // IE8, IE9 ...
    //     ie < 9 // Anything less than IE9
    // ----------------------------------------------------------
    
    // UPDATE: Now using Live NodeList idea from @jdalton
    
    var ie = (function(){
    
        var undef,
            v = 3,
            div = document.createElement('div'),
            all = div.getElementsByTagName('i');
    
        while (
            div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
            all[0]
        );
    
        return v > 4 ? v : undef;
    
    }());
    
    0 讨论(0)
  • 2020-11-22 08:58

    Window runs IE10 will be auto update to IE11+ and will be standardized W3C

    Nowaday, we don't need to support IE8-

        <!DOCTYPE html>
        <!--[if lt IE 9]><html class="ie ie8"><![endif]-->
        <!--[if IE 9]><html class="ie ie9"><![endif]-->
        <!--[if (gt IE 9)|!(IE)]><!--><html><!--<![endif]-->
        <head>
            ...
            <!--[if lt IE 8]><meta http-equiv="Refresh" content="0;url=/error-browser.html"><![endif]--
            ...
        </head>
    
    0 讨论(0)
  • 2020-11-22 08:59

    To detect MSIE (v6 - v7 - v8 - v9 - v10 - v11) easily :

    if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
       // MSIE
    }
    
    0 讨论(0)
  • 2020-11-22 08:59

    This function will return the IE major version number as an integer, or undefined if the browser isn't Internet Explorer. This, like all user agent solutions, is suceptible to user agent spoofing (which has been an official feature of IE since version 8).

    function getIEVersion() {
        var match = navigator.userAgent.match(/(?:MSIE |Trident\/.*; rv:)(\d+)/);
        return match ? parseInt(match[1]) : undefined;
    }
    
    0 讨论(0)
  • 2020-11-22 09:00

    Return IE version or if not IE return false

    function isIE () {
      var myNav = navigator.userAgent.toLowerCase();
      return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
    }
    

    Example:

    if (isIE () == 8) {
     // IE8 code
    } else {
     // Other versions IE or not IE
    }
    

    or

    if (isIE () && isIE () < 9) {
     // is IE version less than 9
    } else {
     // is IE 9 and later or not IE
    }
    

    or

    if (isIE()) {
     // is IE
    } else {
     // Other browser
    }
    
    0 讨论(0)
  • 2020-11-22 09:00

    If nobody else has added an addEventLister-method and you're using the correct browser mode then you could check for IE 8 or less with

    if (window.attachEvent && !window.addEventListener) {
        // "bad" IE
    }
    

    Legacy Internet Explorer and attachEvent (MDN)

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