简体   繁体   English

Internet Explorer 11 检测

[英]Internet Explorer 11 detection

I know IE 11 has different user agent string than all other IE我知道 IE 11 具有与所有其他 IE 不同的用户代理字符串

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

I have tried to detect IE 11 with answer specified for this question'我试图用为这个问题指定的答案来检测 IE 11'

Jquery fail to detect IE 11 Jquery 无法检测到 IE 11

Thats !!navigator.userAgent.match(/Trident\\/7\\./)那是!!navigator.userAgent.match(/Trident\\/7\\./)

But I am getting error Object not found and needs to be re-evaluated.但是我收到错误Object not found and needs to be re-evaluated.

Then I openede developer console in IE11 and tried to access some predefined javascript objects, I am still getting same error.然后我在 IE11 中打开了开发者控制台并尝试访问一些预定义的 javascript 对象,我仍然遇到同样的错误。

I have tried我试过了

navigator.userAgent

window.navigator

console.log('test');

Anyone have any idea about it ?任何人有任何想法吗?

Edit 18 Nov 2016 2016 年 11 月 18 日编辑

This code also work ( for those who prefer another solution , without using ActiveX )此代码也有效(对于那些更喜欢其他解决方案而不使用 ActiveX 的人

var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
  // true on IE11
  // false on Edge and other IEs/browsers.

Original Answer原答案

In order to check Ie11 , you can use this : ( tested)为了检查 Ie11 ,你可以使用这个:(已测试)

(or run this ) (或运行这个

!(window.ActiveXObject) && "ActiveXObject" in window

I have all VMS of IE :我有 IE 的所有 VMS:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Notice : this wont work for IE11 :注意:这不适用于 IE11:

as you can see here , it returns true :正如你在这里看到的,它返回 true :

在此处输入图片说明

So what can we do :所以,我们能做些什么 :

Apparently , they added the machine bit space :显然,他们添加了机器位空间:

ie11 : 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 : 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"

so we can do:所以我们可以这样做:

/x64|x32/ig.test(window.navigator.userAgent)

this will return true only for ie11.这将仅对 ie11 返回 true。

To detect MSIE (from version 6 to 11) quickly:快速检测 MSIE(从版本 6 到 11):

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

I use the following function to detect version 9, 10 and 11 of IE:我使用以下函数来检测 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
}  

All of the above answers ignore the fact that you mention you have no window or navigator :-)以上所有答案都忽略了您提到没有窗口或导航器的事实:-)

Then I openede developer console in IE11然后我在 IE11 中打开了开发者控制台

and thats where it says这就是它所说的

Object not found and needs to be re-evaluated.未找到对象,需要重新评估。

and navigator, window, console, none of them exist and need to be re-evaluated.和导航器,窗口,控制台,它们都不存在,需要重新评估。 I've had that in emulation.我在仿真中遇到过。 just close and open the console a few times.只需关闭并打开控制台几次。

A pretty safe & concise way to detect IE 11 only is检测IE 11 的一种非常安全且简洁的方法是

if(window.msCrypto) {
    // I'm IE11 for sure
}

or something like this或类似的东西

var IE11= !!window.msCrypto;

msCrypto is a prefixed version of the window.crypto object and only implemented in IE 11. msCryptowindow.crypto对象的前缀版本,仅在 IE 11 中实现。
https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto

Okay try this, simple and for IE11 and IE below 11 version好的试试这个,简单,适用于 IE11 和 11 版本以下的 IE

browserIsIE = navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 || navigator.userAgent.toUpperCase().indexOf("MSIE") != -1;

navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 for IE 11 version navigator.userAgent.toUpperCase().indexOf("MSIE") != -1 for IE below 11 version navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1对于 IE 11 版本navigator.userAgent.toUpperCase().indexOf("MSIE") != -1对于 IE 低于 11 版本

 browserIsIE = navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 || navigator.userAgent.toUpperCase().indexOf("MSIE") != -1; console.log('Is IE Browser : '+ browserIsIE)

And how I implemented this以及我是如何实现的

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

This link was helpful .这个链接很有帮助 It contains the javascript code to detect all versions of IE up to IE11.它包含用于检测所有版本的 IE 到 IE11 的 javascript 代码。 I tested the script with IE11 emulator.我用 IE11 模拟器测试了脚本。 To find the IE11 emulator, right-click on the web browser click "Inspect element".要找到 IE11 模拟器,请右键单击 Web 浏览器,然后单击“检查元素”。 At the bottom-left of the page, scroll down the navigation bar and click the desktop icon.在页面左下角,向下滚动导航栏并单击桌面图标。 The "User Agent String" dropdown box contains options to emulate IE6-11. “用户代理字符串”下拉框包含模拟 IE6-11 的选项。

It works.有用。 I just used it some minutes before writing this answer.在写这个答案之前我只是用了几分钟。 Cannot post snapshots - not enough reputation.无法发布快照 - 没有足够的声誉。


This is the code - follow the link to view it again:这是代码 - 按照链接再次查看:

 // 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>

Using this RegExp seems works for IE 10 and IE 11:使用这个 RegExp 似乎适用于 IE 10 和 IE 11:

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

I do not have a IE older than IE 10 to test this.我没有比 IE 10 旧的 IE 来测试这个。

Use Navigator:-使用导航器:-

The navigator is an object that contains all information about the client machine's browser. navigator是一个对象,其中包含有关客户端计算机浏览器的所有信息。

navigator.appName returns the name of the client machine's browser. navigator.appName返回客户端计算机浏览器的名称。

 navigator.appName === 'Microsoft Internet Explorer' || !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie === 1) ? alert("Please dont use IE.") : alert("This is not IE")
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

I found IE11 is giving more than one user agent strings in different environments.我发现 IE11 在不同环境中提供了多个用户代理字符串。

Instead of relying on MSIE , and other approaches, It's better to rely on Trident version与其依赖MSIE和其他方法,不如依赖Trident版本

const isIE11 = userAgent => userAgent.match(/Trident\/([\d.]+)/) ? +userAgent.match(/Trident\/([\d.]+)/)[1] >= 7;

Hope this helps :)希望这可以帮助 :)

For a minimal approach and untill IE officially dies on the August 17th 2021 🥳✌️🎉 I'm using the IE conditional statement in reverse <!--[if !IE]> --><!-- <![endif]--> .对于最小的方法,直到 IE 于 2021 年 8 月 17 日正式死亡 🥳✌️🎉 我正在反向使用 IE 条件语句<!--[if !IE]> --><!-- <![endif]--> .

<style>
:root{display:none!important}
</style>
<!--[if !IE]> -->
<style>
:root{display:initial!important}
</style>
<!-- <![endif]-->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM