[javascript] Internet Explorer 11 detection

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 ?

This question is related to javascript internet-explorer internet-explorer-11

The answer is


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

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


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 :)


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
}  

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


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.


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.

_x000D_
_x000D_
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")
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
_x000D_
_x000D_
_x000D_


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:

_x000D_
_x000D_
// Get IE or Edge browser version_x000D_
var version = detectIE();_x000D_
_x000D_
if (version === false) {_x000D_
  document.getElementById('result').innerHTML = '<s>IE/Edge</s>';_x000D_
} else if (version >= 12) {_x000D_
  document.getElementById('result').innerHTML = 'Edge ' + version;_x000D_
} else {_x000D_
  document.getElementById('result').innerHTML = 'IE ' + version;_x000D_
}_x000D_
_x000D_
// add details to debug result_x000D_
document.getElementById('details').innerHTML = window.navigator.userAgent;_x000D_
_x000D_
/**_x000D_
 * detect IE_x000D_
 * returns version of IE or false, if browser is not Internet Explorer_x000D_
 */_x000D_
function detectIE() {_x000D_
  var ua = window.navigator.userAgent;_x000D_
_x000D_
  // Test values; Uncomment to check result …_x000D_
_x000D_
  // IE 10_x000D_
  // ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';_x000D_
_x000D_
  // IE 11_x000D_
  // ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';_x000D_
_x000D_
  // Edge 12 (Spartan)_x000D_
  // 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';_x000D_
_x000D_
  // Edge 13_x000D_
  // 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';_x000D_
_x000D_
  var msie = ua.indexOf('MSIE ');_x000D_
  if (msie > 0) {_x000D_
    // IE 10 or older => return version number_x000D_
    return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);_x000D_
  }_x000D_
_x000D_
  var trident = ua.indexOf('Trident/');_x000D_
  if (trident > 0) {_x000D_
    // IE 11 => return version number_x000D_
    var rv = ua.indexOf('rv:');_x000D_
    return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);_x000D_
  }_x000D_
_x000D_
  var edge = ua.indexOf('Edge/');_x000D_
  if (edge > 0) {_x000D_
    // Edge (IE 12+) => return version number_x000D_
    return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);_x000D_
  }_x000D_
_x000D_
  // other browser_x000D_
  return false;_x000D_
}
_x000D_
@import url(https://fonts.googleapis.com/css?family=Fira+Mono|Fira+Sans:300);_x000D_
body {_x000D_
  color: black;_x000D_
  background-color: white;_x000D_
  font-family: "Fira Sans", sans-serif;_x000D_
  font-weight: 300;_x000D_
  margin: 0;_x000D_
  padding: 3rem;_x000D_
}_x000D_
_x000D_
h1 {_x000D_
  color: darkgrey;_x000D_
  text-align: center;_x000D_
  font-weight: 300;_x000D_
  font-size: 1.5rem;_x000D_
  line-height: 2rem;_x000D_
}_x000D_
_x000D_
h2 {_x000D_
  text-align: center;_x000D_
  font-weight: 300;_x000D_
  font-size: 4rem;_x000D_
}_x000D_
_x000D_
p {_x000D_
  color: darkgrey;_x000D_
  text-align: center;_x000D_
  font-family: "Fira Mono", monospace;_x000D_
  font-size: 1rem;_x000D_
  line-height: 1.5rem;_x000D_
}
_x000D_
<h1>Detect IE/Edge version with JavaScript.<br> Updated to recognize Internet Explorer 12+ aka Edge.</h1>_x000D_
<h2 id="result">detecting…</h2>_x000D_
<p id="details">n/a</p>
_x000D_
_x000D_
_x000D_


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.


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. */
}

And how I implemented this

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

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to internet-explorer

Support for ES6 in Internet Explorer 11 The response content cannot be parsed because the Internet Explorer engine is not available, or Flexbox not working in Internet Explorer 11 IE and Edge fix for object-fit: cover; "Object doesn't support property or method 'find'" in IE How to make promises work in IE11 Angular 2 / 4 / 5 not working in IE11 Text in a flex container doesn't wrap in IE11 How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript? includes() not working in all browsers

Examples related to internet-explorer-11

CSS Grid Layout not working in IE11 even with prefixes Support for ES6 in Internet Explorer 11 Text in a flex container doesn't wrap in IE11 Using `window.location.hash.includes` throws “Object doesn't support property or method 'includes'” in IE11 IE11 meta element Breaks SVG IE11 Document mode defaults to IE7. How to reset? IE11 prevents ActiveX from running Internet Explorer 11 disable "display intranet sites in compatibility view" via meta tag not working How to set IE11 Document mode to edge as default? Internet Explorer 11- issue with security certificate error prompt