59

What's the least error-prone way to target just IE11 with JavaScript?

Note: This should really only be done for analytics or informing the user what browser they're using. For everything else, there's feature detection.

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
dave1010
  • 14,332
  • 6
  • 64
  • 64
  • 3
    Searching for "IE11 user-agent" will give you blog posts and this article everybody saw : http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/ – Denys Séguret Jul 03 '13 at 12:03
  • @dystroy that blog post doesn't seem to have any detection code. Even hough it does have IE11's (current) UA, many people won't know how to test for it. – dave1010 Jul 03 '13 at 12:52
  • 1
    For reference, here's the same question about IE10: http://stackoverflow.com/questions/9900311/how-do-i-target-only-internet-explorer-10-for-certain-situations-like-internet-e – dave1010 Jul 03 '13 at 12:54
  • IE11 has several user-agent strings: http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx – EricLaw Sep 26 '13 at 19:33
  • I'd love to know what the bug was in the first place. :) – jcreamer898 Sep 16 '14 at 22:18
  • 2
    Reopened - for some reason all votes to reopen this question in the past ended up aging away, and there were *lots* of these reopen votes. Needless to say, the people who left these scathing comments were part of the problem, choosing to complain rather than actually doing anything to try and get the question reopened. In future, if your question isn't getting reopened despite your best efforts to bring community attention to it, and you feel strongly about getting it reopened, feel free to flag it for moderator attention, explain your case, and we'll have a look. – BoltClock Apr 22 '15 at 17:26

7 Answers7

90

The User-agent string for IE 11 is currently this one :

Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

Windows 10 example:

Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko

Which means your can simply test, for versions 11.xx,

var isIE11 = /Trident.*rv[ :]*11\./.test(navigator.userAgent);

As IE10 user agent was

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

it's probably also safe to bet on the fact that now Trident/X is supposed to be the real versionning.

Lars Gyrup Brink Nielsen
  • 3,413
  • 2
  • 27
  • 32
Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
  • 4
    trident refers to the version of MSHTML, rv is the IE version – Paul Zahra Nov 16 '13 at 10:16
  • 5
    I'm using IE virtually via modern.ie and in IE 11.0.9600.x, there's actually a colon, not a space, between rv and the version number: `Mozilla/5.0 (Windows NT 6.1; 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; rv:11.0) like Gecko` Had to switch to this regex: `!!navigator.userAgent.match(/Trident.*rv[ :]?11\./)` – Kevin Jurkowski Dec 11 '13 at 19:33
  • 4
    On win 7, IE 11 it throws as false. – Gopi Dec 30 '13 at 07:33
  • @TechJerk What's `navigator.userAgent` ? – Denys Séguret Dec 30 '13 at 07:34
  • Mozilla/5.0 (Windows NT6.1; WOW64; Trident/7.0; SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30279;.NET CLR 3.0.30729;Media Center PC 6.0;.NET4.0.0c;.NET4.0E;Infopath.2;rv:11.0) like Gecko – Gopi Dec 30 '13 at 08:38
  • I modified as per @KevinJurkowski comment and it worked! Thanks! – Gopi Dec 30 '13 at 08:41
  • 1
    How would I check for "If IE11 and above"? Would this work? `!!navigator.userAgent.match(/Trident.*rv[ :]?[1-9]{2}\./)` Regex is not my strong suit, but using this for compatibility reasons and want the code to execute inside my if statement if it's IE12, IE13, etc. as well. – LeigerGaming Feb 11 '14 at 02:41
  • 1
    NOTE: this will *not* work for IE11 for the desktop on 64-bit Windows 8.1 Update with compatibility view enabled. see http://msdn.microsoft.com/en-us/library/ie/hh869301%28v=vs.85%29.aspx – Don Jul 11 '14 at 14:49
  • What means `!!` please? – Steffi Oct 07 '15 at 15:21
  • @Steffi `!!` changes a `null` to `false`, and any array to `true`. But there's a cleaner solution, I'll edit. – Denys Séguret Oct 08 '15 at 06:39
  • Shouldn't this be `/Trident.*rv[ :]*11\./.test(navigator.userAgent)`? – l.poellabauer Oct 13 '16 at 08:18
28

IE11 keeps "Trident" in it's UA string, but drops MSIE. A simple way to detect the browser is IE11 or above (IE12, IE13, etc) is:

var isAtLeastIE11 = !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));

If you want just IE11 (and you don't want future versions of IE to match), do this:

var isIE11 = !!(navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/rv[ :]11/));
dave1010
  • 14,332
  • 6
  • 64
  • 64
4
var isIE11 = !!navigator.userAgent.match(/Trident\/7.0; rv 11/);

Source: http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/

Florian Margaine
  • 50,873
  • 14
  • 87
  • 110
3

This will set ie to the version of IE, or 0 if none. It'll work for 1 through 11, but may not detect future versions if Microsoft drops the Trident engine.

var ie = 0;
try { ie = navigator.userAgent.match( /(MSIE |Trident.*rv[ :])([0-9]+)/ )[ 2 ]; }
catch(e){}

You may also be interested in my related, more detailed answer here.

Community
  • 1
  • 1
Beejor
  • 6,352
  • 37
  • 29
  • This is the best little script for me. It targets all IE versions. It doesn't target Edge which I didn't need anyway. :) Thanks @Beejor! – cbloss793 Sep 15 '17 at 21:03
3

I use the following pattern to target all IE browsers. You can short it down if you only need IE 11.

 /msie|trident|edge/g.test(navigator.userAgent.toLowerCase());

Good luck!

Fredrik

3

Here's a script you can use to detect any browser:

<script>

  // Opera
  var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

  // Firefox 1.0+
  var isFirefox = typeof InstallTrigger !== 'undefined';

  // Safari 3.0+ "[object HTMLElementConstructor]" 
  var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);

  // Internet Explorer 6-11
  var isIE = /*@cc_on!@*/false || !!document.documentMode;

  // Edge 20+
  var isEdge = !isIE && !!window.StyleMedia;

  // Chrome 1+
  var isChrome = !!window.chrome && !!window.chrome.webstore;

  // Blink engine detection
  var isBlink = (isChrome || isOpera) && !!window.CSS;

  if (isFirefox==true) {
    alert(isFirefox)
    $('.container-fluid').css({"overflow-y":"auto","height":"150%"});  
  }

</script>
Jesse
  • 3,093
  • 6
  • 22
  • 35
2

Try this,

navigator.sayswho= (function(){
   var N= navigator.appName, ua= navigator.userAgent, tem;
   var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
   if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
   M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
   return M;
})();

Source from Browser detection in JavaScript?

Updated for IE=11

Use this

var isIE11 = navigator.userAgent.match(/Trident\/7.0; rv 11.0/);

Read this http://msdn.microsoft.com/en-us/library/ie/bg182625%28v=vs.85%29.aspx

Community
  • 1
  • 1
Rohan Kumar
  • 38,998
  • 11
  • 69
  • 99