Skip to content

Instantly share code, notes, and snippets.

@jrrio
Last active June 7, 2018 06:46
Show Gist options
  • Save jrrio/81ea76e81945771bd5a88e29bea5511b to your computer and use it in GitHub Desktop.
Save jrrio/81ea76e81945771bd5a88e29bea5511b to your computer and use it in GitHub Desktop.
Detecting IE11

Extracted from comp.lang.javascript: Re: Detecting IE11 ?
Data: Wed, 23 May 2018 23:52:33 -0300
De: Joao Rodrigues jr@yahoo.com
Reference: 3120d5a3-6a6a-4a1d-88f5-da7510b252da@googlegroups.com

Is there a good way of detecting IE11 - or, better, any IE ?

Starting with Internet Explorer (IE) 4 until IE 10 in standards mode, we could use Conditional Compilation (CC) for detecting MSIE [1]. Activating CC in JScript was done with the @cc_on statement:

/*@cc_on
  alert("You are using MSIE");
@*/

Dean Edwards once shared this nifty JS code in his blog [2]:

var isMSIE = /*@cc_on!@*/false;

Besides, it was possible to use conditional comments in HTML to load scripts in IE until version 9:

<!--[if IE]>
<script type="text/javascript" src="/js/ie.js"></script>
<![endif]-->

However, as of version 10, IE started to ignore conditional comments [3]:

<!--[if IE]>
This content is ignored in IE10 and other browsers.
In older versions of IE it renders as part of the page.
<![endif]-->

This means conditional comments can still be used, but will only target IE 9 and earlier versions.

To make things more difficult, IE 11 ignores both conditional comment and conditional compilation (@cc_on statement), and doesn't want to be called "Internet Explorer" anymore [4]. So in IE11, Microsoft enforced feature detection.

Even so, it is still possible to use "browser sniffing" for detecting IE 11 as posted on NCZonline [4]. We may search for “Trident” and the “rv” token to identify IE 11 in standards mode - the “Trident” token was introduced with IE 9.

IE11's Default User-Agent String in standards mode:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
                             ^^^^^^^

But notice that users may render a site in Compatibility View (click Tools > Compatibility View Settings), which will make IE send a different User-Agent string [5].

Given the warnings above, "UA sniffing" for IE 11 standards mode could be done as below:

function isIE11() {
  var ua = window.navigator.userAgent;
  return (ua.indexOf("Trident/7.0") > 0);
}

Also there is a more general function on Stack Overflow [6]:

function getInternetExplorerVersion() {
  var rv = -1;
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  } else if (navigator.appName == 'Netscape') {
    var ua = navigator.userAgent;
    var re  = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

console.log('IE version:', getInternetExplorerVersion());

Moreover, you may find other hacks for detecting specific IE versions, by means of activating CC, checking document.documentMode, etc., such as:

if (isMSIE && document.documentMode === 10) {
  alert('MSIE 10');
}

You may look up on Stack Overflow [7] and GitHub Gist [8] to find other hacks.

[1] https://msdn.microsoft.com/en-us/library/8ka90k2e(v=vs.94).aspx
[2] http://dean.edwards.name/weblog/2007/03/sniff/
[3] https://blogs.msdn.microsoft.com/ie/2011/07/06/html5-parsing-in-ie10/
[4] https://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/
[5] https://blogs.msdn.microsoft.com/ieinternals/2013/09/21/internet-explorer-11s-many-user-agent-strings/
[6] https://stackoverflow.com/questions/17907445/how-to-detect-ie11
[7] https://stackoverflow.com/questions/9900311/how-do-i-target-only-internet-explorer-10-for-certain-situations-like-internet-e
[8] https://gist.github.com/zunairmushtaq/aeaa48432d51cad0eb1c

-- João Rodrigues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment