How can I target only Internet Explorer 11 with JavaScript?

Solution 1:

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.

Solution 2:

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/));

Solution 3:

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/

Solution 4:

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.