Detect IE version (prior to v9) in JavaScript

前端 未结 30 1436
半阙折子戏
半阙折子戏 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:45

    This works for me. I use it as a redirect to a page that explains why we don't like < IE9 and provide links to browsers we prefer.

    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;URL=http://google.com">
    <![endif]-->
    
    0 讨论(0)
  • 2020-11-22 08:45

    This approach to detecting IE combines the strengths and avoids the weaknesses of jKey's answer using conditional comments and Owen's answer using user agents.

    • jKey's approach works up to version 9 and immune to user agent spoofing in IE 8 & 9.
    • Owen's approach can fail on IE 5 & 6 (reporting 7) and is susceptible to UA spoofing, but it can detect IE versions >= 10 (now also including 12, which postdates Owen's answer).

      // ----------------------------------------------------------
      // A short snippet for detecting versions of IE
      // ----------------------------------------------------------
      // If you're not in IE (or IE version is less than 5) then:
      //     ie === undefined
      // Thus, to detect IE:
      //     if (ie) {}
      // And to detect the version:
      //     ie === 6 // IE6
      //     ie > 7 // IE8, IE9 ...
      // ----------------------------------------------------------
      var ie = (function(){
          var v = 3,
              div = document.createElement('div'),
              all = div.getElementsByTagName('i');
      
          while (
              div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
              all[0]
          );
          if (v <= 4) { // Check for IE>9 using user agent
              var match = navigator.userAgent.match(/(?:MSIE |Trident\/.*; rv:|Edge\/)(\d+)/);
              v = match ? parseInt(match[1]) : undefined;
          }
          return v;
      }());
      

    This can be used to set useful classes to your document containing the IE version:

        if (ie) {
            document.documentElement.className += ' ie' + ie;
            if (ie < 9)
                document.documentElement.className += ' ieLT9';
        }
    

    Note that it detects the compatibility mode being used, if IE is in compatability mode. Also note that IE version is mostly useful for older versions (<10); higher versions are more standards-compliant and it's probably better to instead check for features using something like modernizr.js.

    0 讨论(0)
  • 2020-11-22 08:46
    var isIE9OrBelow = function()
    {
       return /MSIE\s/.test(navigator.userAgent) && parseFloat(navigator.appVersion.split("MSIE")[1]) < 10;
    }
    
    0 讨论(0)
  • 2020-11-22 08:46
    if (!document.addEventListener) {
        // ie8
    } else if (!window.btoa) {
        // ie9
    }
    // others
    
    0 讨论(0)
  • 2020-11-22 08:47

    Use conditional comments. You're trying to detect users of IE < 9 and conditional comments will work in those browsers; in other browsers (IE >= 10 and non-IE), the comments will be treated as normal HTML comments, which is what they are.

    Example HTML:

    <!--[if lt IE 9]>
    WE DON'T LIKE YOUR BROWSER
    <![endif]-->
    

    You can also do this purely with script, if you need:

    var div = document.createElement("div");
    div.innerHTML = "<!--[if lt IE 9]><i></i><![endif]-->";
    var isIeLessThan9 = (div.getElementsByTagName("i").length == 1);
    if (isIeLessThan9) {
        alert("WE DON'T LIKE YOUR BROWSER");
    }
    
    0 讨论(0)
  • 2020-11-22 08:47

    I do like that:

    <script>
       function isIE () {
           var myNav = navigator.userAgent.toLowerCase();
           return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
       }    
       var ua = window.navigator.userAgent;
       //Internet Explorer | if | 9-11
    
       if (isIE () == 9) {
           alert("Shut down this junk! | IE 9");
       } else if (isIE () == 10){
           alert("Shut down this junk! | IE 10");
       } else if (ua.indexOf("Trident/7.0") > 0) {
           alert("Shut down this junk! | IE 11");
       }else{
           alert("Thank god it's not IE!");
       }
    
    </script>
    
    0 讨论(0)
提交回复
热议问题