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

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


当前回答

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

其他回答

要检测Internet Explorer 10|11,你可以在body标签后立即使用这个小脚本:

在我的情况下,我使用jQuery库加载头部。

<!DOCTYPE HTML>
<html>
<head>
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
    <script>if (navigator.appVersion.indexOf('Trident/') != -1) $("body").addClass("ie10");</script>
</body>
</html>

我喜欢这样:

<script>
   function isIE () {
       var myNav = navigator.userAgent.toLowerCase();
       return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
   }    
   var ua = window.navigator.userAgent;
   //Internet Explorer | if | 9-11

   if (isIE () == 9) {
       alert("Shut down this junk! | IE 9");
   } else if (isIE () == 10){
       alert("Shut down this junk! | IE 10");
   } else if (ua.indexOf("Trident/7.0") > 0) {
       alert("Shut down this junk! | IE 11");
   }else{
       alert("Thank god it's not IE!");
   }

</script>

根据微软的说法,以下是最好的解决方案,它也很简单:

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat( RegExp.$1 );
    }
    return rv;
}

function checkVersion()
{
    var msg = "You're not using Internet Explorer.";
    var ver = getInternetExplorerVersion();

    if ( ver > -1 )
    {
        if ( ver >= 8.0 ) 
            msg = "You're using a recent copy of Internet Explorer."
        else
            msg = "You should upgrade your copy of Internet Explorer.";
      }
    alert( msg );
}

我建议不要再重写这段代码了。我建议你使用Conditionizr库(http://conditionizr.com/),它能够测试特定的IE版本以及其他浏览器、操作系统,甚至是否有视网膜显示。

只包含您需要的特定测试的代码,您还可以获得经过多次迭代的测试库的好处(并且很容易升级而不会破坏您的代码)。

它还可以很好地与Modernizr配合,Modernizr可以处理所有情况,在这些情况下,您最好测试特定的功能,而不是特定的浏览器。

你的代码可以做检查,但正如你所认为的,如果有人试图使用IE v1或> v19访问你的页面将不会得到错误,所以可能更安全的做检查的Regex表达式如下代码:

var userAgent = navigator.userAgent.toLowerCase();
// Test if the browser is IE and check the version number is lower than 9
if (/msie/.test(userAgent) && 
    parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]) < 9) {
  // Navigate to error page
}