Detect IE version (prior to v9) in JavaScript

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

提交回复
热议问题