如何使用 JavaScript 仅针对 Internet Explorer 11?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17447373/
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
How can I target only Internet Explorer 11 with JavaScript?
提问by dave1010
What's the least error-prone way to target just IE11 with JavaScript?
使用 JavaScript 仅针对 IE11 的最不容易出错的方法是什么?
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.
注意:这实际上应该仅用于分析或通知用户他们正在使用的浏览器。对于其他一切,都有特征检测。
回答by Denys Séguret
The User-agent string for IE 11 is currently this one :
IE 11 的用户代理字符串目前是这样的:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko
Windows 10 example:
Windows 10 示例:
Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko
Which means your can simply test, for versions 11.xx,
这意味着您可以简单地测试 11.xx 版本,
var isIE11 = /Trident.*rv[ :]*11\./.test(navigator.userAgent);
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
it's probably also safe to bet on the fact that now Trident/X
is supposed to be the real versionning.
可以肯定的是,现在Trident/X
应该是真正的版本控制。
回答by dave1010
IE11 keeps "Trident" in it's UA string, but drops MSIE. A simple way to detect the browser is IE11 or above (IE12, IE13, etc) is:
IE11 在它的 UA 字符串中保留“Trident”,但丢弃 MSIE。检测浏览器是否为 IE11 或以上(IE12、IE13 等)的简单方法是:
var isAtLeastIE11 = !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));
If you want justIE11 (and you don't want future versions of IE to match), do this:
如果您只想要IE11(并且您不希望 IE 的未来版本匹配),请执行以下操作:
var isIE11 = !!(navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/rv[ :]11/));
回答by Florian Margaine
var isIE11 = !!navigator.userAgent.match(/Trident\/7.0; rv 11/);
Source: http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/
来源:http: //www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/
回答by Fredrik Borggren
I use the following pattern to target all IE browsers. You can short it down if you only need IE 11.
我使用以下模式来定位所有 IE 浏览器。如果您只需要 IE 11,则可以将其缩短。
/msie|trident|edge/g.test(navigator.userAgent.toLowerCase());
Good luck!
祝你好运!
Fredrik
弗雷德里克
回答by baisakhi chauhan
Here's a script you can use to detect any browser:
这是一个可用于检测任何浏览器的脚本:
<script>
// Opera
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';
// Safari 3.0+ "[object HTMLElementConstructor]"
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;
// Chrome 1+
var isChrome = !!window.chrome && !!window.chrome.webstore;
// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;
if (isFirefox==true) {
alert(isFirefox)
$('.container-fluid').css({"overflow-y":"auto","height":"150%"});
}
</script>
回答by Rohan Kumar
Try this,
试试这个,
navigator.sayswho= (function(){
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
return M;
})();
Source from Browser detection in JavaScript?
Updated for IE=11
更新为IE=11
Use this
用这个
var isIE11 = navigator.userAgent.match(/Trident\/7.0; rv 11.0/);
Read this http://msdn.microsoft.com/en-us/library/ie/bg182625%28v=vs.85%29.aspx
阅读此http://msdn.microsoft.com/en-us/library/ie/bg182625%28v=vs.85%29.aspx
回答by Beejor
This will set ie
to the version of IE, or 0 if none. It'll work for 1 through 11, but may not detect future versions if Microsoft drops the Trident engine.
这将设置ie
为 IE 的版本,如果没有,则设置为 0。它适用于 1 到 11,但如果 Microsoft 放弃 Trident 引擎,则可能无法检测到未来的版本。
var ie = 0;
try { ie = navigator.userAgent.match( /(MSIE |Trident.*rv[ :])([0-9]+)/ )[ 2 ]; }
catch(e){}
You may also be interested in my related, more detailed answer here.
您可能也有兴趣在我相关,更详细的答案在这里。