Detect IE version (prior to v9) in JavaScript

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

提交回复
热议问题