我知道IE 11具有与所有其他IE不同的用户代理字符串
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko
我尝试使用为此问题指定的答案检测IE 11
多数民众赞成!!navigator.userAgent.match(/Trident\/7\./)
但我收到了错误
Object not found and needs to be re-evaluated.
然后我在IE11中打开开发者控制台并试图访问一些预定义的javascript对象,我仍然得到同样的错误。
我试过了
navigator.userAgent
window.navigator
console.log('test');
有人对此有任何想法吗?
答案 0 :(得分:140)
2016年11月18日编辑
此代码也有效(适用于那些喜欢其他解决方案而不使用ActiveX 的人)
var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
// true on IE11
// false on Edge and other IEs/browsers.
原始答案
为了检查Ie11,您可以使用:(已测试)
(或运行this)
!(window.ActiveXObject) && "ActiveXObject" in window
我拥有IE的所有VMS:
注意:这不适用于IE11:
正如您在此处所见,它返回true:
那我们该怎么做:
显然,他们添加了机器位空间:
ie11:
"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"
ie12:
"Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"
所以我们可以这样做:
/x64|x32/ig.test(window.navigator.userAgent)
这将仅对ie11返回true。
答案 1 :(得分:85)
快速检测MSIE(从版本6到11):
if(navigator.userAgent.indexOf('MSIE')!==-1
|| navigator.appVersion.indexOf('Trident/') > -1){
/* Microsoft Internet Explorer detected in. */
}
答案 2 :(得分:21)
以上所有答案都忽略了你提到没有窗口或导航器的事实: - )
然后我在IE11中打开开发者控制台
那就是
找不到对象,需要重新评估。
和导航器,窗口,控制台,它们都不存在,需要重新评估。我在仿真中有这个。关闭并打开控制台几次。
答案 3 :(得分:18)
我使用以下函数来检测IE的版本9,10和11:
function ieVersion() {
var ua = window.navigator.userAgent;
if (ua.indexOf("Trident/7.0") > -1)
return 11;
else if (ua.indexOf("Trident/6.0") > -1)
return 10;
else if (ua.indexOf("Trident/5.0") > -1)
return 9;
else
return 0; // not IE9, 10 or 11
}
答案 4 :(得分:5)
我是如何实现这个
的<script type="text/javascript">
!(window.ActiveXObject) && "ActiveXObject"
function isIE11(){
return !!navigator.userAgent.match(/Trident.*rv[ :]*11\./);
}
</script>
答案 5 :(得分:2)
This link was helpful。它包含用于检测IE11的所有IE版本的javascript代码。我用IE11模拟器测试了脚本。要查找IE11仿真器,请右键单击Web浏览器,单击“Inspect element”。在页面左下角,向下滚动导航栏,然后单击桌面图标。 “用户代理字符串”下拉框包含模拟IE6-11的选项。
有效。我只是在写这个答案之前用了几分钟。无法发布快照 - 声誉不够。
这是代码 - 点击链接再次查看:
// Get IE or Edge browser version
var version = detectIE();
if (version === false) {
document.getElementById('result').innerHTML = '<s>IE/Edge</s>';
} else if (version >= 12) {
document.getElementById('result').innerHTML = 'Edge ' + version;
} else {
document.getElementById('result').innerHTML = 'IE ' + version;
}
// add details to debug result
document.getElementById('details').innerHTML = window.navigator.userAgent;
/**
* detect IE
* returns version of IE or false, if browser is not Internet Explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;
// Test values; Uncomment to check result …
// IE 10
// ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
// IE 11
// ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
// Edge 12 (Spartan)
// ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';
// Edge 13
// ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
@import url(https://fonts.googleapis.com/css?family=Fira+Mono|Fira+Sans:300);
body {
color: black;
background-color: white;
font-family: "Fira Sans", sans-serif;
font-weight: 300;
margin: 0;
padding: 3rem;
}
h1 {
color: darkgrey;
text-align: center;
font-weight: 300;
font-size: 1.5rem;
line-height: 2rem;
}
h2 {
text-align: center;
font-weight: 300;
font-size: 4rem;
}
p {
color: darkgrey;
text-align: center;
font-family: "Fira Mono", monospace;
font-size: 1rem;
line-height: 1.5rem;
}
<h1>Detect IE/Edge version with JavaScript.<br> Updated to recognize Internet Explorer 12+ aka Edge.</h1>
<h2 id="result">detecting…</h2>
<p id="details">n/a</p>
答案 6 :(得分:1)
if(window.msCrypto) { /* I'm IE11 for sure */ }
默认情况下,IE11阻止使用ActiveX的功能。因此,除非用户同意运行脚本,否则此代码将不起作用。
答案 7 :(得分:0)
使用此RegExp似乎适用于IE 10和IE 11:
function isIE(){
return /Trident\/|MSIE/.test(window.navigator.userAgent);
}
我没有比IE 10更早的IE来测试它。
答案 8 :(得分:0)
一种非常简洁的方法来仅检测 IE 11 :
if(window.msCrypto) { /* I'm IE11 for sure */ }
或
var IE11= !!window.msCrypto;
msCrypto
是window.crypto
对象的前缀版本,仅在IE 11中实现。
https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
答案 9 :(得分:0)
我发现IE11在不同的环境中提供了多个用户代理字符串。
与其依靠MSIE
和其他方法,不如依靠Trident
版本
const isIE11 = userAgent => userAgent.match(/Trident\/([\d.]+)/) ? +userAgent.match(/Trident\/([\d.]+)/)[1] >= 7;
希望这会有所帮助:)
答案 10 :(得分:0)
好吧,对于IE11和低于11版本的IE,请尝试一下
browserIsIE = navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 || navigator.userAgent.toUpperCase().indexOf("MSIE") != -1;
navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1
(对于IE 11版本)
navigator.userAgent.toUpperCase().indexOf("MSIE") != -1
用于低于11版本的IE
browserIsIE = navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 || navigator.userAgent.toUpperCase().indexOf("MSIE") != -1;
console.log('Is IE Browser : '+ browserIsIE)
答案 11 :(得分:0)
为了最小的方法,直到 IE 于 2021 年 8 月 17 日正式消亡?✌️? 我正在反向使用 IE 条件语句 <!--[if !IE]> --><!-- <![endif]-->
。
<style>
:root{display:none!important}
</style>
<!--[if !IE]> -->
<style>
:root{display:initial!important}
</style>
<!-- <![endif]-->