151

I know IE 11 has different user agent string than all other 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'

Jquery fail to detect IE 11

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

But I am getting error 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.

I have tried

navigator.userAgent

window.navigator

console.log('test');

Anyone have any idea about it ?

3

12 Answers 12

225

Edit 18 Nov 2016

This code also work (for those who prefer another solution , without using 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)

(or run this)

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

I have all VMS of IE :

enter image description here

enter image description here

enter image description here

enter image description here

Notice : this wont work for IE11 :

as you can see here , it returns true :

enter image description here

So what can we do :

Apparently , they added the machine bit space :

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"

so we can do:

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

this will return true only for ie11.

14
  • 1
    @Jan. yes. only IE11 would return true for this whole condition. I just showed that <11 return false.
    – Royi Namir
    Commented Jun 12, 2014 at 7:59
  • 1
    @IanSteffy This is just to show the result(!) of the operation. You can open a HTML file and add script tag and run the command inside that script. Here run this.
    – Royi Namir
    Commented Oct 13, 2015 at 7:53
  • 2
    x64 returns both Chrome and Edge
    – Gene R
    Commented Oct 25, 2016 at 6:49
  • 4
    Wait, there's an IE12? I thought IE11 was the last one and that we soon won't have to worry about that stupid browser anymore! ☹ Commented Jul 9, 2018 at 0:25
  • 5
    Does not work for my IE 11.0.9600.19431 on Windows 7 Enterprise. There is no window.MSInputMethodContext. Commented Nov 14, 2019 at 11:53
118

To detect MSIE (from version 6 to 11) quickly:

if(navigator.userAgent.indexOf('MSIE')!==-1
|| navigator.appVersion.indexOf('Trident/') > -1){
   /* Microsoft Internet Explorer detected in. */
}
5
  • 4
    Shouldn't the second indexOf() be > -1?
    – reformed
    Commented Jun 21, 2018 at 19:03
  • Yeah, I don't know why it's >0 and not >-1, but "Trident/" shows up much farther into the appVersion string anyways-
    – chrismarx
    Commented Sep 14, 2018 at 17:05
  • This answer does not detect IE 11 specifically which is what the OP asked
    – Alexey
    Commented Mar 26, 2021 at 17:43
  • Why not both >= 0 or > -1 ? Should be the same outcome but more readable / straightforward.
    – Khazl
    Commented Apr 20, 2021 at 9:19
  • Note that appVersion is deprecated: developer.mozilla.org/en-US/docs/Web/API/Navigator/appVersion
    – TrueWill
    Commented Jan 11, 2022 at 23:19
26

I use the following function to detect version 9, 10 and 11 of IE:

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
}  
22

All of the above answers ignore the fact that you mention you have no window or navigator :-)

Then I openede developer console in 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.

3
  • 5
    goodness gracious THANK YOU for actually reading the question and answering the problem with re-evaluation.
    – marknadal
    Commented Jun 16, 2015 at 1:27
  • 9
    Worked for me too. This is why there will be partying around the world when IE dies.
    – voltrevo
    Commented Feb 3, 2016 at 3:37
  • Closing and opening the console worked for me. Strange behavior compared to Firefox or Chrome (but it's IE, so that figures.)
    – Ectropy
    Commented Oct 3, 2016 at 19:05
17

A pretty safe & concise way to detect IE 11 only is

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.
https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto

1
  • Thanks, I used the reverse: if(window.crypto) since msCrypto wasn't detected in @types
    – MDMoore313
    Commented Jul 30, 2021 at 0:04
10

Okay try this, simple and for IE11 and IE below 11 version

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

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

console.log('Is IE Browser : '+ browserIsIE)

4

And how I implemented this

<script type="text/javascript">
  !(window.ActiveXObject) && "ActiveXObject"
  function isIE11(){
    return !!navigator.userAgent.match(/Trident.*rv[ :]*11\./);
  }
</script>
2
  • 1
    I think you have a typo in your function. First, you do the condition check, which is not used. Second, perhaps you mean && "ActiveXObject" in window. Third: what the trick with double negation !! ?
    – dma_k
    Commented Jun 9, 2016 at 9:08
  • 2
    !! pretty much means 'Coerce to boolean'. Commented Nov 22, 2016 at 15:47
2

This link was helpful . It contains the javascript code to detect all versions of IE up to IE11. I tested the script with IE11 emulator. To find the IE11 emulator, right-click on the web browser click "Inspect element". 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.

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>

1

Using this RegExp seems works for IE 10 and IE 11:

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

I do not have a IE older than IE 10 to test this.

1

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]-->.

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

Use Navigator:-

The navigator is an object that contains all information about the client machine's browser.

navigator.appName returns the name of the client machine's browser.

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>

1
  • 1
    appName is Netscape on IE 11. They did everything they could to trick developers.
    – kagronick
    Commented Jun 7, 2019 at 20:13
0

I found IE11 is giving more than one user agent strings in different environments.

Instead of relying on MSIE, and other approaches, It's better to rely on Trident version

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

Hope this helps :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.