如果我们网站的用户使用的是v9之前版本的Internet Explorer,我希望将他们弹出到一个错误页面。不值得我们花时间和金钱去支持iev9之前的版本。所有其他非ie浏览器的用户都没问题,不应该被弹出。以下是提议的代码:

if(navigator.appName.indexOf("Internet Explorer")!=-1){     //yeah, he's using IE
    var badBrowser=(
        navigator.appVersion.indexOf("MSIE 9")==-1 &&   //v9 is ok
        navigator.appVersion.indexOf("MSIE 1")==-1  //v10, 11, 12, etc. is fine too
    );

    if(badBrowser){
        // navigate to error page
    }
}

这段代码能行吗?

为了阻止一些可能会出现在我面前的评论:

Yes, I know that users can forge their useragent string. I'm not concerned. Yes, I know that programming pros prefer sniffing out feature-support instead of browser-type but I don't feel this approach makes sense in this case. I already know that all (relevant) non-IE browsers support the features that I need and that all pre-v9 IE browsers don't. Checking feature by feature throughout the site would be a waste. Yes, I know that someone trying to access the site using IE v1 (or >= 20) wouldn't get 'badBrowser' set to true and the warning page wouldn't be displayed properly. That's a risk we're willing to take. Yes, I know that Microsoft has "conditional comments" that can be used for precise browser version detection. IE no longer supports conditional comments as of IE 10, rendering this approach absolutely useless.

还有其他明显需要注意的问题吗?


当前回答

如果没有人添加addeventlister -方法,而你使用的是正确的浏览器模式,那么你可以检查ie8或更低版本

if (window.attachEvent && !window.addEventListener) {
    // "bad" IE
}

传统Internet Explorer和attachEvent (MDN)

其他回答

为了可靠地过滤IE8及更老版本,可以使用检查全局对象:

if (document.all && !document.addEventListener) {
    alert('IE8 or lower');
}

这种检测IE的方法结合了jKey使用条件注释的答案和Owen使用用户代理的答案的优点,并避免了它们的缺点。

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; }());

这可以用来为包含IE版本的文档设置有用的类:

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

注意,如果IE处于兼容模式,它会检测正在使用的兼容模式。还要注意的是,IE版本主要适用于旧版本(<10);更高的版本更符合标准,使用modernizr.js之类的东西检查功能可能会更好。

不要把这么简单的事情过分复杂化。只需使用一个简单的JScript条件注释。它是最快的,因为它不需要向非IE浏览器添加代码来进行检测,而且它的兼容性可以追溯到支持HTML条件注释之前的IE版本。简而言之,

var IE_version=(-1/*@cc_on,@_jscript_version@*/);

小心小注释:大多数(如果不是全部)会将特殊条件注释误认为常规注释,并将其删除

基本上,上面的代码将IE_version的值设置为您正在使用的IE版本,或者-1(如果您没有使用IE)。现场演示:

var IE_version = (1 / * @cc_on @_jscript_version@ * /); 如果(IE_version ! = = 1) { 文档。write("<h1>You are using Internet Explorer " + IE_version + "</h1>"); }其他{ 文档。write("<h1>您没有使用低于11的Internet Explorer版本</h1>"); }

这是基于条件注释只在旧版本的Internet Explorer中可见这一事实,而IE将@_jscript_version设置为浏览器的版本。例如,如果你使用Internet Explorer 7,那么@_jscript_version将被设置为7,因此,将执行的后处理javascript实际上是这样的:

var IE_version=(-1,7);

结果是7。

var isIE9OrBelow = function()
{
   return /MSIE\s/.test(navigator.userAgent) && parseFloat(navigator.appVersion.split("MSIE")[1]) < 10;
}

如果你需要删除IE浏览器版本,那么你可以按照下面的代码。这段代码适用于IE6到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>