How to detect IE11?

231,545

Solution 1

IE11 no longer reports as MSIE, according to this list of changes it's intentional to avoid mis-detection.

What you can do if you really want to know it's IE is to detect the Trident/ string in the user agent if navigator.appName returns Netscape, something like (the untested);

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

Note that IE11 (afaik) still is in preview, and the user agent may change before release.

Solution 2

Use !(window.ActiveXObject) && "ActiveXObject" in window to detect IE11 explicitly.

To detect any IE (pre-Edge, "Trident") version, use "ActiveXObject" in window instead.

Solution 3

Use MSInputMethodContext as part of a feature detection check. For example:

//Appends true for IE11, false otherwise
window.location.hash = !!window.MSInputMethodContext && !!document.documentMode;

References

Solution 4

I've read your answers and made a mix. It seems to work with Windows XP(IE7/IE8) and Windows 7 (IE9/IE10/IE11).

function ie_ver(){  
    var iev=0;
    var ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
    var trident = !!navigator.userAgent.match(/Trident\/7.0/);
    var rv=navigator.userAgent.indexOf("rv:11.0");

    if (ieold) iev=new Number(RegExp.$1);
    if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
    if (trident&&rv!=-1) iev=11;

    return iev;         
}

Of course if I return 0, means no IE.

Solution 5

Get IE Version from the User-Agent

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

How it works: The user-agent string for all IE versions includes a portion "MSIE space version" or "Trident other-text rv space-or-colon version". Knowing this, we grab the version number from a String.match() regular expression. A try-catch block is used to shorten the code, otherwise we'd need to test the array bounds for non-IE browsers.

Note: The user-agent can be spoofed or omitted, sometimes unintentionally if the user has set their browser to a "compatibility mode". Though this doesn't seem like much of an issue in practice.


Get IE Version without the User-Agent

var d = document, w = window;
var ie = ( !!w.MSInputMethodContext ? 11 : !d.all ? 99 : w.atob ? 10 : 
d.addEventListener ? 9 : d.querySelector ? 8 : w.XMLHttpRequest ? 7 : 
d.compatMode ? 6 : w.attachEvent ? 5 : 1 );

How it works: Each version of IE adds support for additional features not found in previous versions. So we can test for the features in a top-down manner. A ternary sequence is used here for brevity, though if-then and switch statements would work just as well. The variable ie is set to an integer 5-11, or 1 for older, or 99 for newer/non-IE. You can set it to 0 if you just want to test for IE 1-11 exactly.

Note: Object detection may break if your code is run on a page with third-party scripts that add polyfills for things like document.addEventListener. In such situations the user-agent is the best option.


Detect if the Browser is Modern

If you're only interested in whether or not a browser supports most HTML 5 and CSS 3 standards, you can reasonably assume that IE 8 and lower remain the primary problem apps. Testing for window.getComputedStyle will give you a fairly good mix of modern browsers, as well (IE 9, FF 4, Chrome 11, Safari 5, Opera 11.5). IE 9 greatly improves on standards support, but native CSS animation requires IE 10.

var isModernBrowser = ( !document.all || ( document.all && document.addEventListener ) ); 
Share:
231,545

Related videos on Youtube

Paul Sweatte
Author by

Paul Sweatte

To know that you do not know is the best To pretend to know when you do not know is a disease The clever combatant looks to the effect of combined energy, and does not require too much from individuals. Hence his ability to pick out the right men and utilize combined energy The fool doth think he is wise, but the wise man knows himself to be a fool He who exercises no forethought but makes light of his opponents is sure to be captured by them In dispelling ignorance, it is not favorable to act as an enemy To bear with fools in kindliness brings good fortune In punishing folly It does not further one To commit transgressions The only thing that furthers Is to prevent transgressions All day long the superior man is creatively active At nightfall his mind is still beset with cares If the superior man undertakes something and tries to lead, he goes astray; But if he follows, he finds guidance. The superior man understands the signs of the time and prefers to desist The wisdom of a learned man cometh by opportunity of leisure; and he that hath little business shall become wise You are equal to others, but you are not better Quiet perseverance brings good fortune Look to your conduct and weigh the favorable signs When everything is fulfilled, supreme good fortune comes Know what is in front of your face, and what is hidden from you will be disclosed to you. Don't let your heart get big because of your knowledge. Take counsel with the ignorant as well as with the scholar The greatest epiphany can be lost to the simplest distraction The surest way to remember something is to give it rhythm Earnestness is the path of immortality, thoughtlessness the path of death Physical objects are not in space, but these objects are spatially extended And that you remit the debt as alms. That would be better for you if you understand The price of reliability is the pursuit of the utmost simplicity. It is a price which the very rich find most hard to pay It's a curious thing about our industry: not only do we not learn from our mistakes, we also don't learn from our successes The tools we use have a profound influence on our thinking habits, and therefore, on our thinking abilities There will never be a universal programming language since no one language is best for all problems Now I realize that I am the power that commands the feeling of my mind and from which circumstances grow Remember that man created method, and method did not create man, and do not strain yourself in twisting into someone’s preconceived pattern, which unquestionably would be appropriate for him, but not necessarily for you What is not subject to choice is fact It's not that the malicious code understands, they just need to match a pattern 1 × 0; he who falls on this stone will be broken 1 ÷ 0; but on whomever it falls, it will grind them to powder ∞ ϕ=find Q;A appears

Updated on June 26, 2021

Comments

  • Paul Sweatte
    Paul Sweatte about 3 years

    When I want to detect IE I use this code:

    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 );
      }
      return rv;
    }
    function checkVersion()
    {
      var msg = "You're not using Internet Explorer.";
      var ver = getInternetExplorerVersion();
    
      if ( ver > -1 )
      {
        msg = "You are using IE " + ver;
      }
      alert( msg );
    }
    

    But IE11 is returning "You're not using Internet Explorer". How can I detect it?

    • dave1010
      dave1010 over 10 years
    • Ludwig
      Ludwig about 10 years
      Or stackoverflow.com/questions/9514179/… for complete detection
    • David G
      David G about 10 years
      Anything based on user agent is flawed. It's too easy to spoof, Now, it may be that this is a not a problem, but it seems to me that a browser detection script should have a fair chance of detecting masquerading. I use a combination of conditional comments, fall-through to trying to coerce the document.documentMode and then look at window.MSInputMethodContext as per Paul Sweatte below. I'd post my code but it's flogging a dead horse.
    • razor
      razor over 9 years
      IE11 has user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko Os types: 6.1 - win7, 6.3 - win81
    • Royi Namir
      Royi Namir over 9 years
    • xorcus
      xorcus about 9 years
      here's the best solution I've found: stackoverflow.com/a/20201867/2047385 if (Object.hasOwnProperty.call(window, "ActiveXObject") && !window.ActiveXObject) { // is IE11 }
    • JoshYates1980
      JoshYates1980 over 7 years
      another option: gist.github.com/CodeHeight/3ad11c1daf8907a53506b869a2251119.‌​js
  • Izkata
    Izkata over 10 years
    it's intentional to avoid mis-detection - Sadly, now that IE11 is released, we have code that is broken in only IE11, whereas a correct detection of IE would have worked...
  • rg89
    rg89 over 10 years
    I converted this solution to a Boolean if the version is less important function isIE() { return ((navigator.appName == 'Microsoft Internet Explorer') || ((navigator.appName == 'Netscape') && (new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})").exec(navigator‌​.userAgent) != null))); }
  • Mark Avenius
    Mark Avenius over 10 years
    @lzkata - Per the html5 spec here, IE is actually following the standard. So yes, it's intentional, but it is per the new standard (deprecating the old html api.)
  • Avatar
    Avatar over 10 years
    "the reason they did this was deliberate. They wanted to break browser detection scripts like this." from stackoverflow.com/a/18872067/1066234 ... Actually it should be: 'They wanted to make billion websites break like this.'
  • Avatar
    Avatar over 10 years
    This works for me: var isIE11 = !!navigator.userAgent.match(/Trident\/7\./); source
  • eivers88
    eivers88 over 10 years
    I used your answer in a solution I created for developing and emulating in IE11. Thought I'd share: github.com/eivers88/IE11-Missing-Conditional-Solution
  • cartland
    cartland over 10 years
    Best not to use literals in new RegExp(). In this case "[\.0-9]" should be "[\\.0-9]". i.e. there's an unescaped backslash. Best to use the // form (as Echt does) instead. It doesn't require escaping backslash, and it's clearer to the parsers/linters it's meant to be a regexp rather than just a string.
  • mcw
    mcw about 10 years
    Omu - which version of Chrome? Both lines return false for me with Chrome 35.0.1916.114 m
  • User
    User about 10 years
    Wrong code because Acoo browser uses "MSIE" in the useragent. Look at useragentstring.com/pages/Acoo%20Browser
  • Jan.
    Jan. about 10 years
    @Omu just tested in chrome 35.0.1916.114 on linux and it returns ''false''
  • Alex W
    Alex W about 10 years
    Some versions of IE 11 actually do report MSIE, so watch out for false positives. Also, detecting Trident/ will definitely not be reliable once IE 12 comes out.
  • David G
    David G about 10 years
    This seems to me to be more robust. Certainly anything based on user agent is pretty useless.
  • Krunal
    Krunal about 10 years
    WRONG ARGUMENT. I have tested across all IE browsers, even Win8 devices as well.
  • User
    User about 10 years
    You tested IE11 browser but not Acoo browser and Acoo browser uses "MSIE" in the useragent like i say'd you so the IEold also detects Acoo browser as IEold (The old version of IE) and i'm sure that Acoo browser uses "MSIE" in the useragent because i've did navigator.userAgent on a javascript test site with Acoo browser (Test site: w3schools.com)
  • Krunal
    Krunal almost 10 years
    Can you please give a better solution for this?
  • User
    User almost 10 years
    U can use !navigator.userAgent.match("Acoo Browser;") && navigator.userAgent.match(/MSIE/i) ? true : false but that does not always work because acoo browser does not always have "Acoo Browser;" in its useragent but actually u don't need to care about that acoo browser has "MSIE" in its useragent because acoo browser is almost thesame.
  • Farid Nouri Neshat
    Farid Nouri Neshat almost 10 years
    Just tested it. Works perfectly. I think 'ActiveXObject' in window is good enough for detecting any IE version.
  • Eadel
    Eadel almost 10 years
    Nice approach, but doesn't work if you select other than default User Agent String in Emulation tab of IE11, leaving document mode default (Edge).
  • Alan
    Alan almost 10 years
    This Microsoft article suggests this solution may no longer work msdn.microsoft.com/en-us/library/ie/dn423948(v=vs.85).aspx
  • mcw
    mcw almost 10 years
    Actually, this article describes the reason why my method works. Attempting to access window.ActiveXObject, as described in the article, returns undefined in IE11 now (as well as non-Microsoft browsers.) The test using the javascript in operator returns true in all Microsoft browsers, so both are the case strictly in IE11. If Microsoft issues a change to the behavior of the in operator, yes, this method will break.
  • Tarık Özgün Güner
    Tarık Özgün Güner over 9 years
    That's worked instead of ActiveXObject. Thanks a lot
  • Chemical Programmer
    Chemical Programmer about 9 years
    It also returns trun on IE 10 (Win 7)
  • xorcus
    xorcus about 9 years
    here's the best solution I've found: stackoverflow.com/a/20201867/2047385 if (Object.hasOwnProperty.call(window, "ActiveXObject") && !window.ActiveXObject) { // is IE11 }
  • Paul Sweatte
    Paul Sweatte almost 9 years
    @tdakhla Updated to filter out IE Edge.
  • Neo
    Neo almost 9 years
    "ActiveXObject" in window returns False in Edge.
  • omer
    omer almost 9 years
    the 'Get IE Version from the User-Agent' work nicely! just to be sure, this will show all version including IE11?
  • Beejor
    Beejor almost 9 years
    @omer Yes, because it looks for the string "MSIE" or "Trident"; the latter is used by IE 11 and above. So it will work for all future IE versions until MS changes the name of its browser engine. I believe the new Edge browser still uses Trident.
  • dragonore
    dragonore over 8 years
    This works great for embeded PDF objects as well when Internet Explorer did their changes.
  • Robin
    Robin over 8 years
    why !! symbol is used
  • Jake Rowsell
    Jake Rowsell over 8 years
    If you are using a regex to chec you can add the i flag to make it case insensitive, rather than .toLowerCase(). There is also no need for the .toString() method.
  • Vivian River
    Vivian River over 8 years
    Using IE 11, I noticed that any variable named isIE will always be false, even when it has been set to true. They thought of everything!
  • mastazi
    mastazi about 8 years
    @Neo Edge is not IE, the OP's question was how to detect IE11
  • Neo
    Neo about 8 years
    @mastazi yea but in this answer, it is mentioned that ActiveXObject can be used to detect any IE version. It is though debatable that should Edge be called a version of IE or not (Microsoft surely does not want to call it one), but for many of the developers IE has become the synonym for any default Microsoft browser.
  • AlbatrossCafe
    AlbatrossCafe about 8 years
    My favorite - accounts for IE6-10 and IE11. I added in a check for edge as well
  • Jon Davis
    Jon Davis almost 8 years
    This answer should be removed. This was working but lately it has been literally crashing IE for many users who are updating IE11.
  • mcw
    mcw almost 8 years
    @stimpy77 - just tested in 11.420.10586.0 and Edge 25.10586.0.0. Not seeing a crash - can you be more specific?
  • Jon Davis
    Jon Davis almost 8 years
    It may be environmental ("works on MY machine"), but no browser API behavior should be environmentally distinct. window.ActiveXObject is an unsafe API to begin with so this is unsurprising.
  • j4v1
    j4v1 almost 8 years
    Where can I find the new standard global objects added to new versions of Edge? I infer Math.acosh is one of those.
  • James Peter
    James Peter almost 8 years
    @j4v1 Math.acosh Only is Supported in Microsoft Edge (Edge browser). Ref: msdn.microsoft.com/en-us/en-en/library/dn858239(v=vs.94).asp‌​x
  • Shaun
    Shaun almost 8 years
    Important to note for any of these answers that might seem like they're not working is to check the Emulation settings, I was going to mark this answer as incorrect but then I checked emulation and saw that some intranet compatibility settings were overriding the display mode, once that was taken out of the equation this solution worked fine for me.
  • BernardV
    BernardV over 7 years
    This works great, thanks @Beejor! I implemented a simple redirect to another page using your answer: var ie = 0; try { ie = navigator.userAgent.match( /(MSIE |Trident.*rv[ :])([0-9]+)/ )[ 2 ]; } catch(e){} if (ie !== 0) { location.href = "../ie-redirect/redirect.html"; }
  • Beejor
    Beejor about 7 years
    @BernardV Looks good! A quick tip: if you have access to the server, detecting the user-agent in a script there may work better, since the user wouldn't notice any redirect/flicker, less HTTP requests, etc. But in a pinch doing it with JS works too.
  • ruffin
    ruffin about 7 years
    var isIE11 = navigator.userAgent.indexOf("Trident/7") > -1 seems most Occam-y. @Robin: What !! means, aka, "Kai could've left !! out."
  • danjah
    danjah about 7 years
    Just confirmed #false in non-IE, IE8,9,10, Edge 14,15. #true in IE11 only. Did not test with document mode active. Tested with Browserstack.
  • Thijs
    Thijs about 7 years
    This method stoped working for me. In the past it correctly detected IE11 but now that I have Internet Explorer version 11.1198.14393.0 (Update version 11.0.42 (KB4018271)) running on Windows 10 Enterprise (version 1607, OS Build 14393.1198) Math seems to support the acosh method.
  • James Peter
    James Peter about 7 years
    @Thijs In Windows 10 Education with IE11 v 11.1198.14393.0 I tested successfully. You should try another math function according to ES6.
  • Thijs
    Thijs about 7 years
    @JamesPeter I've landed upon checking for document.documentMode. This seems to be a more reliable property to check for IE11 or Edge. documentMode exists in IE11 but no longer exists in Edge.
  • James Peter
    James Peter about 7 years
    Yes, documentMode exists since IE8-10, and document.compatMode exists since it exists since IE5- *. However the latter is disapproved from IE8 but is still used. Thanks for sharing.
  • Matthias Hryniszak
    Matthias Hryniszak over 6 years
    I might be blind but the following regular expressions are just wrong: "MSIE ([0-9]{1,}[\.0-9]{0,})" "Trident/.*rv:([0-9]{1,}[\.0-9]{0,})" in both cases when there is a check for zero or more dot or number then since this is a double-quoted string they should read: "MSIE ([0-9]{1,}[.0-9]{0,})" "Trident/.*rv:([0-9]{1,}[.0-9]{0,})" Just came across this after turning the no-useless-escape ESLint rule. So it worked just because in character class matcher dot is a dot (not just any character) and a \. is also a dot. So maybe not wrong but that escaping for dot is not necessary
  • KSPR
    KSPR about 6 years
    This detects Firefox as This is IE 0
  • jackhowa
    jackhowa over 4 years
    This is working for me. Have a basic helper const isIE11 = () => !!window.MSInputMethodContext && !!document.documentMode;