Detect IE version (prior to v9) in JavaScript

前端 未结 30 1370
半阙折子戏
半阙折子戏 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 09:06

    I made a convenient underscore mixin for this.

    _.isIE();        // Any version of IE?
    _.isIE(9);       // IE 9?
    _.isIE([7,8,9]); // IE 7, 8 or 9?
    

    _.mixin({
      isIE: function(mixed) {
        if (_.isUndefined(mixed)) {
          mixed = [7, 8, 9, 10, 11];
        } else if (_.isNumber(mixed)) {
          mixed = [mixed];
        }
        for (var j = 0; j < mixed.length; j++) {
          var re;
          switch (mixed[j]) {
            case 11:
              re = /Trident.*rv\:11\./g;
              break;
            case 10:
              re = /MSIE\s10\./g;
              break;
            case 9:
              re = /MSIE\s9\./g;
              break;
            case 8:
              re = /MSIE\s8\./g;
              break;
            case 7:
              re = /MSIE\s7\./g;
              break;
          }
    
          if (!!window.navigator.userAgent.match(re)) {
            return true;
          }
        }
    
        return false;
      }
    });
    
    console.log(_.isIE());
    console.log(_.isIE([7, 8, 9]));
    console.log(_.isIE(11));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

    0 讨论(0)
  • 2020-11-22 09:07

    If you need to delect IE Browser version then you can follow below code. This code working well for version IE6 to IE11

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click on Try button to check IE Browser version.</p>
    
    <button onclick="getInternetExplorerVersion()">Try it</button>
    
    <p id="demo"></p>
    
    <script>
    function getInternetExplorerVersion() {
       var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");
            var rv = -1;
    
            if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer, return version number
            {               
                if (isNaN(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))))) {
                    //For IE 11 >
                    if (navigator.appName == 'Netscape') {
                        var ua = navigator.userAgent;
                        var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
                        if (re.exec(ua) != null) {
                            rv = parseFloat(RegExp.$1);
                            alert(rv);
                        }
                    }
                    else {
                        alert('otherbrowser');
                    }
                }
                else {
                    //For < IE11
                    alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
                }
                return false;
            }}
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 09:08

    Detecting IE version using feature detection (IE6+, browsers prior to IE6 are detected as 6, returns null for non-IE browsers):

    var ie = (function (){
        if (window.ActiveXObject === undefined) return null; //Not IE
        if (!window.XMLHttpRequest) return 6;
        if (!document.querySelector) return 7;
        if (!document.addEventListener) return 8;
        if (!window.atob) return 9;
        if (!document.__proto__) return 10;
        return 11;
    })();
    

    Edit: I've created a bower/npm repo for your convenience: ie-version

    Update:

    a more compact version can be written in one line as:

    return window.ActiveXObject === undefined ? null : !window.XMLHttpRequest ? 6 : !document.querySelector ? 7 : !document.addEventListener ? 8 : !window.atob ? 9 : !document.__proto__ ? 10 : 11;
    
    0 讨论(0)
  • 2020-11-22 09:10

    Conditional comments are no longer supported in IE as of Version 10 as noted on the Microsoft reference page.

    var ieDetector = function() {
      var browser = { // browser object
    
          verIE: null,
          docModeIE: null,
          verIEtrue: null,
          verIE_ua: null
    
        },
        tmp;
    
      tmp = document.documentMode;
      try {
        document.documentMode = "";
      } catch (e) {};
    
      browser.isIE = typeof document.documentMode == "number" || eval("/*@cc_on!@*/!1");
      try {
        document.documentMode = tmp;
      } catch (e) {};
    
      // We only let IE run this code.
      if (browser.isIE) {
        browser.verIE_ua =
          (/^(?:.*?[^a-zA-Z])??(?:MSIE|rv\s*\:)\s*(\d+\.?\d*)/i).test(navigator.userAgent || "") ?
          parseFloat(RegExp.$1, 10) : null;
    
        var e, verTrueFloat, x,
          obj = document.createElement("div"),
    
          CLASSID = [
            "{45EA75A0-A269-11D1-B5BF-0000F8051515}", // Internet Explorer Help
            "{3AF36230-A269-11D1-B5BF-0000F8051515}", // Offline Browsing Pack
            "{89820200-ECBD-11CF-8B85-00AA005B4383}"
          ];
    
        try {
          obj.style.behavior = "url(#default#clientcaps)"
        } catch (e) {};
    
        for (x = 0; x < CLASSID.length; x++) {
          try {
            browser.verIEtrue = obj.getComponentVersion(CLASSID[x], "componentid").replace(/,/g, ".");
          } catch (e) {};
    
          if (browser.verIEtrue) break;
    
        };
        verTrueFloat = parseFloat(browser.verIEtrue || "0", 10);
        browser.docModeIE = document.documentMode ||
          ((/back/i).test(document.compatMode || "") ? 5 : verTrueFloat) ||
          browser.verIE_ua;
        browser.verIE = verTrueFloat || browser.docModeIE;
      };
    
      return {
        isIE: browser.isIE,
        Version: browser.verIE
      };
    
    }();
    
    document.write('isIE: ' + ieDetector.isIE + "<br />");
    document.write('IE Version Number: ' + ieDetector.Version);

    then use:

    if((ieDetector.isIE) && (ieDetector.Version <= 9))
    {
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:10

    For ie 10 and 11:

    You can use js and add a class in html to maintain the standard of conditional comments:

      var ua = navigator.userAgent,
          doc = document.documentElement;
    
      if ((ua.match(/MSIE 10.0/i))) {
        doc.className = doc.className + " ie10";
    
      } else if((ua.match(/rv:11.0/i))){
        doc.className = doc.className + " ie11";
      }
    

    Or use a lib like bowser:

    https://github.com/ded/bowser

    Or modernizr for feature detection:

    http://modernizr.com/

    0 讨论(0)
  • 2020-11-22 09:10

    Detecting IE and its versions couldn't be easier, and all you need is a bit of native/vanilla Javascript:

    var uA = navigator.userAgent;
    var browser = null;
    var ieVersion = null;
    
    if (uA.indexOf('MSIE 6') >= 0) {
        browser = 'IE';
        ieVersion = 6;
    }
    if (uA.indexOf('MSIE 7') >= 0) {
        browser = 'IE';
        ieVersion = 7;
    }
    if (document.documentMode) { // as of IE8
        browser = 'IE';
        ieVersion = document.documentMode;
    }
    

    And this is a way to use it:

    if (browser == 'IE' && ieVersion <= 9) 
        document.documentElement.className += ' ie9-';
    

    .

    Works in all IE versions, including higher versions in lower Compatability View/Mode, and documentMode is IE proprietary.

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