如何使用JavaScript最可靠地只针对IE11?
注意:这实际上只应用于分析或通知用户正在使用的浏览器。对于其他所有情况,都应该使用功能检测。
如何使用JavaScript最可靠地只针对IE11?
注意:这实际上只应用于分析或通知用户正在使用的浏览器。对于其他所有情况,都应该使用功能检测。
IE 11的用户代理字符串目前为:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko
Windows 10示例:
Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko
这意味着你可以简单地测试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
视为真正的版本号。
rv
和浏览器版本号之间实际上是冒号而不是空格。因此,我需要使用这个正则表达式:!!navigator.userAgent.match(/Trident.*rv[ :]?11\./)
- Kevin Jurkowski!!navigator.userAgent.match(/Trident.*rv[ :]?[1-9]{2}\./)
正则表达式不是我的强项,但出于兼容性考虑,我希望代码在if语句中执行,如果是IE12、IE13等版本。 - LeigerGamingIE11保留了"Trident"在它的UA字符串中,但删除了MSIE。检测浏览器是否为IE11或以上版本(如IE12、IE13等)的简单方法是:
var isAtLeastIE11 = !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));
如果你只需要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/
Internet Explorer 11不应再被称为“IE”。在此版本中,微软已经开始向用户展示他们所信仰的“互操作性标准”的承诺,这意味着IE成为过去式,我们应该把它叫做“Trident”(IE引擎的名称)或“IE11”。ie
设置为IE的版本,如果没有则为0。它适用于1到11版,但如果Microsoft放弃Trident引擎,则可能无法检测到未来的版本。var ie = 0;
try { ie = navigator.userAgent.match( /(MSIE |Trident.*rv[ :])([0-9]+)/ )[ 2 ]; }
catch(e){}
这里有一个可以用来检测任何浏览器的脚本:
<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>
我使用以下模式来针对所有IE浏览器。如果您只需要IE 11,则可以将其缩短。
/msie|trident|edge/g.test(navigator.userAgent.toLowerCase());
祝你好运!
Fredrik
试一下这个:
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;
})();
更新至IE=11
使用以下代码:
var isIE11 = navigator.userAgent.match(/Trident\/7.0; rv 11.0/);
请阅读此链接,其中讲解了有关IE(Web浏览器)的各种技术。