Detect IE version (prior to v9) in JavaScript

前端 未结 30 1987
半阙折子戏
半阙折子戏 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));

提交回复
热议问题