【问题标题】:Detect if any kind of IE (MSIE) [duplicate]检测是否有任何类型的 IE (MSIE) [重复]
【发布时间】:2014-09-11 17:18:00
【问题描述】:

我不想让用户使用 Microsoft Internetexplorer(任何版本)访问我的网站。

到目前为止,我发现的是检测它是否低于或等于版本 10。

一件很烦人的事:Internetexplorer >v10 不承认自己是 InternetExplorer。

到目前为止我发现并尝试了什么:

if(navigator.appVersion.indexOf("MSIE")!=-1){
alert("You use IE. That´s no good.");
}

if ( $.browser.msie ) {
alert( $.browser.version );
}

http://msdn.microsoft.com/en-us/library/ie/ms537509%28v=vs.85%29.aspx

如果有javascript、jquery或php,我会使用任何解决方案。

【问题讨论】:

  • 你为什么要这么做?
  • 好问题! +1.. 我还能做些什么来帮助人们远离 ie?
  • if($.browser.msie) 没用吗?
  • 不相信这是重复的 +1 :)
  • 这不是重复的。这是一个合法的问题。非常适合在 Intranet 等上使用。

标签: javascript php jquery internet-explorer


【解决方案1】:

这对我来说可以检测任何版本的 IE 5-11 (Internet Explorer)(2014 年 8 月 5 日)

if (navigator.appName == 'Microsoft Internet Explorer' ||  !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie == 1))
{
  alert("Please dont use IE.");
}

【讨论】:

  • 仅供参考,截至 2015 年 8 月,这对我不起作用。有趣(令人讨厌),navigator.appName 设置为 Netscape 而不是 Microsoft Internet Explorer。
  • 最好也检查一下 IE 的版本。
  • navigator.userAgent.match(/rv:11/) 你必须修复这个,因为它在你的正则表达式字符串中不包含: 标记
  • 我确认最新版本的 IE(不是 Edge)将“Netscape”作为 navigator.appName。
  • 有史以来最好的警报信息。 !!三天后让我的生活变得悲惨
【解决方案2】:

这是因为 Internet Explorer 的每个版本都会更新 user-agent string

MSIE 标记已在 Internet Explorer 11 中删除,$.browser 使用 navigator.userAgent 来确定平台,并在 jQuery 1.9 中删除。

您可以使用下面的代码来确定纯java-script的浏览器。

var isIE = !!navigator.userAgent.match(/Trident/g) || !!navigator.userAgent.match(/MSIE/g);

if(isIE){
 alert("IE"); 
}
else{
 alert("Not IE");   
}

谢谢!

【讨论】:

  • 这就是他在他的问题中已经写的......不适用于> 10
  • 是的,对不起,我已经更新了答案。
  • 为什么代码使用双重否定? (!!)
  • @rinogo 我自己也想知道这一点,以前从未见过这种模式。发现将值设为布尔值,因为 .match() 返回结果数组,没有它们,isIE 将只是 ["Trident"] 或 ["MSIE"]。也可以这样写:var isIE = /Trident/.test(navigator.userAgent) || /MSIE/.test(navigator.userAgent);
  • const isIE = /Trident|MSIE/.test(navigator.userAgent); 是较短的等效项
【解决方案3】:

如果您对用户当前使用的 ie 版本不感兴趣,您可以尝试通过检测浏览器是否支持 Conditional Compilation Statements 来让它工作

http://msdn.microsoft.com/en-us/library/7kx09ct1%28v=vs.80%29.aspx

if(/*@cc_on!@*/false)
{
    // You use IE. That´s no good.
    alert("oh my god");
}

【讨论】:

    【解决方案4】:

    您可以使用conditional compilation ,例如

    <script>
    var isIE = false;
    /*@cc_on isIE = true; @*/
    </script>
    

    但请注意,IE11 在标准模式下不会遵守这一点。用户代理嗅探通常是一个坏主意,但随着 IE 变得更加符合标准,它也变得更难检测(希望也意味着更少需要)

    【讨论】:

      【解决方案5】:

      对于当前为 IE 11 的 IE> 10,用户代理在浏览器的 HTTP 请求标头中携带一些内容

      Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) 像 Gecko

      您可以检查版本 11 的“rv:11.0”。如果您需要此代码,请告诉我。

      【讨论】:

        【解决方案6】:

        我过去曾发现(也许在 SO)这个脚本,它对我有用(IE 10 也是)

        <![if IE]>
        <script type='text/javascript'>
        if(/*@cc_on!@*/false)
        var bIsIE = 1;
        </script>
        <![endif]>
        

        然后

        if (typeof (bIsIE) != 'undefined')
        {
            //IE :(
        }
        else
        {
            //NOT IE :)
        }
        

        【讨论】:

        • 不管有没有兼容模式都不适用于 IE11
        猜你喜欢
        • 2010-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-25
        • 1970-01-01
        • 2016-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多