如何在 Asp.net 中使用 javascript 检测 IE 11 [英] How to detect IE 11 with javascript in Asp.net

查看:21
本文介绍了如何在 Asp.net 中使用 javascript 检测 IE 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想检测浏览器,IE 8 或更高版本适合我.为此,我使用了以下代码,但对于 IE 11 却失败了.对于其他它检测正确.

Hello I want to detect the Browser , IE 8 or more will be appropriate for me. For this i used following code but it fails for IE 11 . For other its detecting properly.

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

下面是我也试过但没有成功的链接.

Below is the link which also I tried but couldn't succeed.

推荐答案

不要做浏览器检测!它会坏掉,会给你带来问题.

DO NOT DO BROWSER DETECTION! It will break, and it will cause you problems.

IE11 具有与以前的 IE 版本完全不同的用户代理字符串;它不再包含MSIE"文本.这就是您的检测代码不起作用的原因.

IE11 has a completely different User Agent string to previous IE versions; it not longer includes the "MSIE" text. This is why your detection code doesn't work.

重要的是要注意,他们这样做的原因是故意的.他们想要像这样破坏浏览器检测脚本.

It's important to note here that the reason they did this was deliberate. They wanted to break browser detection scripts like this.

可以更改您的代码以使用 IE11,但我强烈建议您不要这样做,因为当 IE12 出现时,您可能会再次遇到同样的问题.

It is possible to change your code to work with IE11, but I strongly recommend not doing this, as you'll probably have the same issue all over again when IE12 comes out.

那么他们为什么要破坏浏览器检测脚本?简单:因为IE11没有以前版本的bug,而且新增了很多功能.因此,如果您进行浏览器检测是因为 IE 存在某些错误或缺失的功能,并且您有基于浏览器检测来修复这些问题的代码,那么该代码实际上可能会在 IE11 中导致更严重的问题,而无需修复.

So why did they want to break browser detection scripts? Simple: Because IE11 does not have the bugs of previous versions, and it has a lot of new features. So if you're doing browser detection because IE has certain bugs or missing features and you've got code to fix those issues based on the browser detect, then that code might actually cause worse problems in IE11 where the fixes aren't needed.

IE11 破坏了你的脚本,但同样的逻辑适用于所有浏览器和所有版本;检测浏览器和版本几乎总是错误的做法.

IE11 has broken your script, but the same logic applies to all browsers and all versions; detecting the browser and version is almost always the wrong thing to do.

如果您想支持某些特定功能,但在较旧的 IE 版本(或其他较旧的浏览器)中没有,请不要使用浏览器检测来解决;您应该改用特征检测.

If there are specific features that you want to support, but are missing in older IE versions (or other older browsers), don't use browser detection to work it out; you should use feature detection instead.

功能检测意味着检查浏览器是否支持您要使用的特定功能.最常用的方法是使用 Modernizr 库.他们网站上的文档将指导您完成设置.

Feature detection means checking the browser to see whether it supports the particular features you want to use. The most common way of doing this is to use the Modernizr library. The docs on their site will guide you through setting it up.

旧版IE存在一些难以检测的错误,对于这几种情况,将浏览器检测作为最后手段是有效的,但这些情况实际上仅适用于IE6及更早版本.也许偶尔为 IE7.但是您已经在问题中声明您只查看 IE8 及更高版本,因此这不适用.

There are a few bugs in older IE versions which are hard to detect, and for these few cases, it is valid to use browser detection as a last resort, but these cases are really only for IE6 and earlier. Maybe occasionally for IE7. But you've stated in the question that you're only looking at IE8 and later, so that should not apply.

坚持特征检测;它更可靠,更好的实践,并且不会在新的浏览器版本发布时突然中断.

Stick with feature detection; it's more reliable, better practice, and won't break suddenly when a new browser version is released.

这篇关于如何在 Asp.net 中使用 javascript 检测 IE 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆