2014-02-17 165 views
57

我知道IE 11比不同的用户代理字符串其他所有IE的Internet Explorer 11的检测

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

我试图发现IE 11与这个问题”

Jquery fail to detect 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

@Bobkhin我上面我的问题提到。得到错误'对象未找到,需要重新评估。' –

+0

可能重复[如何在Asp.net中使用javascript检测IE 11](http://stackoverflow.com/questions/18871760/how-to-detect -ie-11-with-javascript-in-asp-net) – RandomSeed

+0

[How to detect IE11?](http://stackoverflow.com/questions/17907445/how-to-detect-ie11) –

回答

82

编辑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:

enter image description here

enter image description here

enter image description here

enter image description here

注意:对于IE12这不会工作:

,你可以在这里看到,它返回true:

enter image description here

所以,我们能做些什么:

很显然,他们增加了机器位空间:

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) 

,它只对IE12返回true。

+0

Doesn'这也导致其他IE浏览器? – PeeHaa

+1

@PeeHaa没有..... IE 11,将是唯一的谁通过左边的部分 –

+0

你可以请在开发者控制台尝试它,让我知道 –

55

为了检测MSIE(从版本6至11)快速:

if(navigator.userAgent.indexOf('MSIE')!==-1 
|| navigator.appVersion.indexOf('Trident/') > 0){ 
    /* Microsoft Internet Explorer detected in. */ 
} 
4

我如何实现这个

<script type="text/javascript"> 
    !(window.ActiveXObject) && "ActiveXObject" 
    function isIE11(){ 
    return !!navigator.userAgent.match(/Trident.*rv[ :]*11\./); 
    } 
</script> 
+1

我认为你的函数中有一个错字。首先,你做条件检查,这是不使用。其次,也许你是指窗口中的&&“ActiveXObject”。第三:双重否定'!!'的诀窍是什么? –

+1

!!非常意味着'胁迫布尔'。 –

10

我使用下面的函数来检测版本9,第10和IE的11 :

function ieVersion() { 
    var ua = window.navigator.userAgent; 
    if (ua.indexOf("Trident/7.0") > 0) 
     return 11; 
    else if (ua.indexOf("Trident/6.0") > 0) 
     return 10; 
    else if (ua.indexOf("Trident/5.0") > 0) 
     return 9; 
    else 
     return 0; // not IE9, 10 or 11 
} 
13

以上所有答案都忽略了您提到您没有窗口或导航器的事实:-)

然后我openede开发者控制台在IE11

和多数民众赞成在那里说

对象没有找到,需要进行重新评估。

和导航器,窗口,控制台,它们都不存在,需要重新评估。我已经在模拟。关闭并打开控制台几次。

+2

善良亲切感谢您真正阅读问题并回答问题与重新评估。 – marknadal

+3

也为我工作。这就是为什么IE死后世界各地将会聚会的原因。 – voltrevo

+0

关闭并打开控制台为我工作。奇怪的行为与Firefox或Chrome相比(但它是IE,所以数字)。 – Ectropy

0

使用这个正则表达式似乎作品IE 10和IE 11:

function isIE(){ 
    return /Trident\/|MSIE/.test(window.navigator.userAgent); 
} 

我没有IE浏览器比IE 10以上才能进行测试。

1

This link was helpful。它包含JavaScript代码来检测所有版本的IE到IE11。我用IE11模拟器测试了脚本。要查找IE11模拟器,请右键单击Web浏览器,然后单击“检查元素”。在页面的左下方,向下滚动导航栏并单击桌面图标。 “用户代理字符串”下拉框包含用于模拟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>