Jquery 无法检测到 IE 11
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18684099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery fail to detect IE 11
提问by Ehud Grand
Just stumbled upon an issue. When trying to detect IE 11 (the beta version currently on air) using Jquery, the result is 'firefox'. The same code detect IE 10. I need to know what browser the user is using in order to display different instructions.
刚刚偶然发现一个问题。当尝试使用 Jquery 检测 IE 11(目前正在播出的测试版)时,结果是“firefox”。相同的代码检测 IE 10。我需要知道用户正在使用什么浏览器才能显示不同的指令。
I am testing in Oracle VirtualBox if it matters. The OS is Win 7.
如果重要,我正在 Oracle VirtualBox 中进行测试。操作系统是Win 7。
Here's the code:
这是代码:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
var browser = function() {
if ($.browser.msie) return "ie";
var ua = navigator.userAgent.toLowerCase();
if ($.browser.mozilla/* && /firefox/.test(ua)*/) return "firefox";
if (/chrome/.test(ua)) return "chrome";
return /*"#"*/'unknown';
} ();
alert (browser); // This return firefox
alert ($.browser.version); // This returns 11.0 - the CORRECT version of IE
</script>
As you can see, Jquery can find the browser version, but not the browser name. Any idea how to bypass it?
如您所见,Jquery 可以找到浏览器版本,但无法找到浏览器名称。知道如何绕过它吗?
回答by Ehud Grand
The final solution:
最终的解决方案:
if (!!navigator.userAgent.match(/Trident\/7\./))
return "ie";
We can only hope that the release version will act the same.
我们只能希望发布版本的行为相同。
回答by mason
It's for compatibility reasons. Client code often performs browser detection instead of feature detection (which is a poor practice). So in an effort to make sure that clients properly use all of IE 11's capabilities Microsoft has made it so that IE 11 will report that it's Mozilla compatible.
这是出于兼容性原因。客户端代码通常执行浏览器检测而不是功能检测(这是一种糟糕的做法)。因此,为了确保客户端正确使用 IE 11 的所有功能,Microsoft 已将其设置为 IE 11 将报告它与 Mozilla 兼容。
So instead of doing browser detection, do feature detection. See Browser detection versus feature detection. There's some great libraries for that, with Modernizrprobably being the most well known (and Microsoft ships it as part of the ASP.NET templates in Visual Studio).
因此,与其进行浏览器检测,不如进行特征检测。请参阅浏览器检测与功能检测。有一些很棒的库,其中Modernizr可能是最著名的(微软将它作为 Visual Studio 中的 ASP.NET 模板的一部分发布)。
回答by Dave Methvin
The purpose of jQuery Migrate is to allow old badly-written code to run, not to encourage writing new badly-written code. Since that old badly-written code was created long before IE11 was released, it doesn't know about IE11 anyway and will probably misbehave regardless. The jQuery Migrate plugin won't be changed to detect IE11. If you are writing new code, don't use browser detection. Instead, use feature detection.
jQuery Migrate 的目的是让旧的写得不好的代码运行,而不是鼓励写新的写得不好的代码。由于那些写得很糟糕的旧代码早在 IE11 发布之前就已创建,因此无论如何它都不了解 IE11,并且无论如何都可能会行为不端。jQuery Migrate 插件不会更改为检测 IE11。如果您正在编写新代码,请不要使用浏览器检测。相反,使用特征检测。
回答by David Fregoli
jQuery.browser
is long deprecated and has been removed, you should use $.support
or a better tool like Modernizr
jQuery.browser
早已弃用并已被删除,您应该使用$.support
或更好的工具,例如Modernizr