Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I target only Internet Explorer 11 with JavaScript?

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.

like image 268
dave1010 Avatar asked Jul 03 '13 11:07

dave1010


People also ask

How do I target IE only with CSS?

Internet Explorer 10 & 11 : Create a media query using -ms-high-contrast, in which you place your IE 10 and 11-specific CSS styles. Because -ms-high-contrast is Microsoft-specific (and only available in IE 10+), it will only be parsed in Internet Explorer 10 and greater.

How do I add JavaScript to Internet Explorer?

Internet ExplorerClick Tools > Internet Options. Click the Security tab > Custom Level. In the Scripting section, click Enable for Active Scripting. In the dialog box that displays, click Yes.

Does ie11 support JavaScript?

Internet Explorer 11 doesn't support JavaScript versions later than ES5. If you want to use the syntax and features of ECMAScript 2015 or later, or TypeScript, you have two options as described in this article. You can also combine these two techniques.


3 Answers

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.

like image 129
7 revs, 3 users 76% Avatar answered Oct 03 '22 13:10

7 revs, 3 users 76%


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/)); 
like image 39
dave1010 Avatar answered Oct 03 '22 12:10

dave1010


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>
like image 23
baisakhi chauhan Avatar answered Oct 03 '22 14:10

baisakhi chauhan