Ahmad
Ahmad

Reputation: 12737

How to properly detect IE11 or later?

I am running a Windows 7 (64 bit) with IE 11 enter image description here

having the following navigator.userAgent:

"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2;
.NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0;
.NET4.0C; .NET4.0E)"

I want to be able to detect the version of IE before I can display anything on my site. In other words, the company I work at have updated most of the computers to run IE11 or Chrome. But some computers still have IE9.

I want my site to work properly for people running IE11 or chrome. Any other version of the browser should be detected and the user informed to update his machine.

All the code I found on SO references v11 being part of the userAgent string, but that is not the case here.

Edit: I also tried:

var isIE11 = !!(navigator.userAgent.match(/Trident/) 
             && !navigator.userAgent.match(/MSIE/));  
         //value is false in IE6/IE9/IE11

var isIE11 = !!(navigator.userAgent.match(/Trident/) 
                 && navigator.userAgent.match(/rv 11/));   
          //value is false in IE6/IE9/IE11

var isIE11 = !(window.ActiveXObject) && "ActiveXObject" in window;
           //value is false in IE6/IE9/IE11

What can be done to detect IE11?

Edit 2: this link http://kangax.github.io/compat-table/es6/ has a way to check hoisted ... feature that only runs on IE11. So I also tried this: enter image description here

{ function f() { return 1; } }
  function g() { return 1; }
{ function g() { return 2; } }
{ function h() { return 1; } }
  function h() { return 2; }

alert( f() === 1 && g() === 2 && h() === 1);  // alerts false in all ie versions

Upvotes: 1

Views: 1368

Answers (3)

Xotic750
Xotic750

Reputation: 23502

If only IE browsers are connecting to the page, then test for hoisted block-level function declaration as only supported in IE11+

function hoistTest() {
    // Note: only available outside of strict mode.
    { function f() { return 1; } }
      function g() { return 1; }
    { function g() { return 2; } }
    { function h() { return 1; } }
      function h() { return 2; }
    
    return f() === 1 && g() === 2 && h() === 1;
}

document.getElementById('out').appendChild(document.createTextNode('hoisted block-level function declaration: ' + hoistTest()));
<pre id="out"></pre>

Update: screenshot of it working on IE11 enter image description here

Upvotes: 2

darioo
darioo

Reputation: 47193

Check that Trident engine version is 7.0; earlier versions of Internet explorer have earlier Trident engine versions.

Examples:

  • Internet explorer 10 has Trident 6.0
  • Internet explorer 9 has Trident 5.0

Source: https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx

Do note that you have to take special care regarding non desktop PC Internet explorer versions (Lumia mobile phones, Xbox etc.).

Also, the newest version of Microsoft's browser, Edge, doesn't use the Trident version anymore.

Upvotes: -1

michelem
michelem

Reputation: 14590

This returns true ONLY in IE11:

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

Upvotes: 1

Related Questions