如果我们网站的用户使用的是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.

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


当前回答

检测IE及其版本再容易不过了,你所需要的只是一点原生/香草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;
}

下面是一种用法:

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

.

适用于所有IE版本,包括更高版本的低兼容性视图/模式,documentMode是IE专有的。

其他回答

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

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

传统Internet Explorer和attachEvent (MDN)

此函数将返回IE主版本号为整数,如果浏览器不是Internet Explorer,则未定义。像所有用户代理解决方案一样,这很容易受到用户代理欺骗(这是IE版本8以来的官方功能)。

function getIEVersion() {
    var match = navigator.userAgent.match(/(?:MSIE |Trident\/.*; rv:)(\d+)/);
    return match ? parseInt(match[1]) : undefined;
}

下面是AngularJS检查IE的方法

/**
 * documentMode is an IE-only property
 * http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx
 */
var msie = document.documentMode;

if (msie < 9) {
    // code for IE < 9
}

这是我喜欢的做法。它提供了最大限度的控制。(注意:条件语句仅在IE5 - 9中支持。)

首先正确地设置ie类

<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>    <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>    <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->    
<head>

然后你可以使用CSS来创建样式异常,或者,如果你需要,你可以添加一些简单的JavaScript:

(function ($) {
    "use strict";

    // Detecting IE
    var oldIE;
    if ($('html').is('.lt-ie7, .lt-ie8, .lt-ie9')) {
        oldIE = true;
    }

    if (oldIE) {
        // Here's your JS for IE..
    } else {
        // ..And here's the full-fat code for everyone else
    }

}(jQuery));

感谢Paul Irish。

function getIEVersion(){
     if (/MSIE |Trident\//.test( navigator.userAgent )=== false) return -1;
    /**[IE <=9]*/
    var isIE9L = typeof ( window.attachEvent ) === 'function' && !( Object.prototype.toString.call( window.opera ) == '[object Opera]' ) ? true : false;
    var re;
    if(isIE9L){
        re = new RegExp( "MSIE ([0-9]{1,}[\.0-9]{0,})" );
        if(re.exec( navigator.userAgent ) !== null)
            return parseFloat( RegExp.$1 );
        return -1;
    }
    /**[/IE <=9]*/
    /** [IE >= 10]*/
    if(navigator.userAgent.indexOf( 'Trident/' ) > -1){
        re = new RegExp( "rv:([0-9]{1,}[\.0-9]{0,})" );
        if(re.exec( navigator.userAgent ) !== null)
            return parseFloat( RegExp.$1 );
        return -1;
    }
    /**[/IE >= 10]*/
    return -1;
};

点击这里==>

var ieVersion = getIEVersion();

if(ieVersion < 0){
    //Not IE
}
//A version of IE

了解关于浏览器导航器的更多信息 https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator