1

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
Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • Probably this might help http://stackoverflow.com/questions/18907131/detecting-ie11-using-css-properties – Vincent1989 Jun 17 '15 at 07:21
  • @Vincent1989 thanks, but I have already been to that answer. My case is unique. The ie I am having is 11, but the userAgent says nothing to indicate that. That is why it does not work for me – Ahmad Jun 17 '15 at 07:37
  • Why do you want to detect the browser type/version? The recommended way is to detect features and only implement them if available. See also http://stackoverflow.com/questions/21391593/what-is-a-reliable-way-to-detect-a-clients-browser-and-its-version-number/21465991#21465991 – Xotic750 Jun 17 '15 at 07:50
  • @Xotic750 what is one thing ie11 can do (in javascript) that prior version cant? – Ahmad Jun 17 '15 at 08:13
  • For example IE11+ supports `hoisted block-level function declaration` (chrome does not currently, firefox does) and this feature can be tested. – Xotic750 Jun 17 '15 at 08:13
  • how can I test `hoisted .....` @Xotic750? – Ahmad Jun 17 '15 at 08:14
  • http://kangax.github.io/compat-table/es6/ see Annex B – Xotic750 Jun 17 '15 at 08:16
  • @Xotic750 I see that IE11 says `YES` on that row, and IE7 says `NO` but I don't know how to test for it even after clicking on the hyperlink in that row and reading the paragraph. No JS code there – Ahmad Jun 17 '15 at 08:23
  • Hover you mouse over the grey circle with a white C in it. – Xotic750 Jun 17 '15 at 08:23
  • @Xotic750 I tried that. Did not work. I updated my question. See last edit – Ahmad Jun 17 '15 at 08:35
  • See my answer and screenshot of it working – Xotic750 Jun 17 '15 at 08:42

3 Answers3

2

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

Xotic750
  • 22,914
  • 8
  • 57
  • 79
1

This returns true ONLY in IE11:

!(window.ActiveXObject) && "ActiveXObject" in window
michelem
  • 14,430
  • 5
  • 50
  • 66
  • Tried that. Gave `false` in all version of IE i tested on. I updated my question to give this info – Ahmad Jun 17 '15 at 07:19
-1

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.

darioo
  • 46,442
  • 10
  • 75
  • 103
  • the userAgent for my ie11 returns a string containing Trident/7 and that also happens when I run ie7 (I get Trident/7) again for ie7 – Ahmad Jun 17 '15 at 07:39