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 ?

Community
  • 1
  • 1
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
  • @Bobkhin I have mentioned above my issue. getting error `Object not found and needs to be re-evaluated.` – Miqdad Ali Feb 17 '14 at 09:59
  • possible duplicate of [How to detect IE 11 with javascript in Asp.net](http://stackoverflow.com/questions/18871760/how-to-detect-ie-11-with-javascript-in-asp-net) – RandomSeed Mar 02 '14 at 23:52
  • possible duplicate of [How to detect IE11?](http://stackoverflow.com/questions/17907445/how-to-detect-ie11) – Paul Sweatte Mar 12 '14 at 20:35

12 Answers12

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.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • Doesn't that also result in other IE's? – PeeHaa Feb 17 '14 at 09:28
  • Could you please try it in developer console and let me know – Miqdad Ali Feb 17 '14 at 09:41
  • @MiqdadAli Didn't you see my pics ? – Royi Namir Feb 17 '14 at 09:41
  • Worked for me with Developer Console compatibility mode in IE11. Thanks, Namir! – justian17 Feb 18 '14 at 15:50
  • Just wondering: in your screenshots you only show !(window.ActiveXObject) and not the "ActiveXObject in window" part.. so does the whole thing returns true in IE11? – Jan. Jun 12 '14 at 07:59
  • 1
    @Jan. yes. only IE11 would return true for this whole condition. I just showed that <11 return false. – Royi Namir Jun 12 '14 at 07:59
  • That may work as suggested. Just keep in mind that future IE12 may also return true. – Shahar Mar 07 '15 at 10:56
  • No browser ive tried on windows or mac seems to allow the live 'javascript:!(window.ActiveXObject)' to be added to the url – Ian Steffy Oct 13 '15 at 07:52
  • 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](http://jsbin.com/remuya/1/edit?html,css,js,output). – Royi Namir Oct 13 '15 at 07:53
  • 2
    `x64` returns both Chrome and Edge – Gene R Oct 25 '16 at 06:49
  • Why are you using 2 "!" in your first code? Is that used to force returning a boolean value? – SyncroIT Jan 18 '18 at 14:12
  • 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! ☹ – Michael Scheper Jul 09 '18 at 00:25
  • There is no IE12! Why is nobody apart from Michael Scheper questioning this response? Does the poster, Royi Namir, mean IE11 or Edge rather than IE12?? Or is this supposed to be some hypothetical future version of IE? – Zeek2 Aug 06 '18 at 07:17
  • 5
    Does **not** work for my IE 11.0.9600.19431 on Windows 7 Enterprise. There is no `window.MSInputMethodContext`. – Heinrich Ulbricht Nov 14 '19 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. */
}
Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
EpokK
  • 38,062
  • 9
  • 61
  • 69
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
}  
Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
KennyE
  • 513
  • 6
  • 15
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.

commonpike
  • 10,499
  • 4
  • 65
  • 58
  • 5
    goodness gracious THANK YOU for actually reading the question and answering the problem with re-evaluation. – marknadal Jun 16 '15 at 01:27
  • 9
    Worked for me too. This is why there will be partying around the world when IE dies. – voltrevo Feb 03 '16 at 03:37
  • Closing and opening the console worked for me. Strange behavior compared to Firefox or Chrome (but it's IE, so that figures.) – Ectropy Oct 03 '16 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

j.j.
  • 1,914
  • 15
  • 12
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)
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
4

And how I implemented this

<script type="text/javascript">
  !(window.ActiveXObject) && "ActiveXObject"
  function isIE11(){
    return !!navigator.userAgent.match(/Trident.*rv[ :]*11\./);
  }
</script>
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
  • 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 Jun 09 '16 at 09:08
  • 2
    !! pretty much means 'Coerce to boolean'. – Andrew Gray Nov 22 '16 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>
aghwotu
  • 402
  • 6
  • 12
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.

antoineMoPa
  • 854
  • 10
  • 20
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]-->
amarinediary
  • 4,930
  • 4
  • 27
  • 45
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>
Parth Raval
  • 4,097
  • 3
  • 23
  • 36
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 :)

Mr.7
  • 2,292
  • 1
  • 20
  • 33