[英]How can I target only Internet Explorer 11 with JavaScript?
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. 对于其他一切,都有功能检测。
The User-agent string for IE 11 is currently this one : IE 11的User-agent字符串当前是这样的:
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);
As IE10 user agent was 作为IE10用户代理
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
应该是真正的版本,这可能也是安全的。
IE11 keeps "Trident" in it's UA string, but drops MSIE. IE11在它的UA字符串中保留“Trident”,但是删除了MSIE。 A simple way to detect the browser is IE11 or above (IE12, IE13, etc) is: 一种检测浏览器的简单方法是IE11或以上(IE12,IE13等):
var isAtLeastIE11 = !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));
If you want just IE11 (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/));
var isIE11 = !!navigator.userAgent.match(/Trident\/7.0; rv 11/);
资料来源: http : //www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/
I use the following pattern to target all IE browsers. 我使用以下模式来定位所有IE浏览器。 You can short it down if you only need IE 11. 如果您只需要IE 11,可以将其缩短。
/msie|trident|edge/g.test(navigator.userAgent.toLowerCase());
Good luck! 祝好运!
Fredrik 弗雷德里克
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? 来自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
This will set ie
to the version of IE, or 0 if none. 这将设置ie
到IE的版本,如果没有则设置为0。 It'll work for 1 through 11, but may not detect future versions if Microsoft drops the Trident engine. 它适用于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 . 您还可能有兴趣在我相关,更详细的答案在这里 。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.