如何使用 JavaScript 仅针对 Internet Explorer 11? [英] How can I target only Internet Explorer 11 with JavaScript?

查看:25
本文介绍了如何使用 JavaScript 仅针对 Internet Explorer 11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 JavaScript 仅针对 IE11 的最不容易出错的方法是什么?

What's the least error-prone way to target just IE11 with JavaScript?

注意:这实际上应该仅用于分析或通知用户他们正在使用的浏览器.对于其他一切,还有特征检测.

Note: This should really only be done for analytics or informing the user what browser they're using. For everything else, there's feature detection.

推荐答案

IE 11 的 User-agent 字符串目前是这个 :

The User-agent string for IE 11 is currently this one :

Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

Windows 10 示例:

Windows 10 example:

Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko

这意味着您可以简单地测试 11.xx 版本,

Which means your can simply test, for versions 11.xx,

var isIE11 = /Trident.*rv[ :]*11./.test(navigator.userAgent);

因为 IE10 用户代理是

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

现在Trident/X 应该是真正的版本控制,这可能也是安全的.

it's probably also safe to bet on the fact that now Trident/X is supposed to be the real versionning.

这篇关于如何使用 JavaScript 仅针对 Internet Explorer 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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