How to detect IE11?
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
- MSInputMethodContext Object
- Input Method Editor API
- What I’d like to see in IE12: Internet Explorer 12 Wishlist of Bug Fixes and New Features
- TypeScript/lib.dom.d.ts at master · Microsoft/TypeScript IE DOM APIs
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 ) );
Related videos on Youtube
![Paul Sweatte](https://i.stack.imgur.com/uLSIj.png?s=256&g=1)
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, 2021Comments
-
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 over 10 years
-
Ludwig about 10 yearsOr stackoverflow.com/questions/9514179/… for complete detection
-
David G about 10 yearsAnything 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 over 9 yearsIE11 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 over 9 yearssee my answer here stackoverflow.com/questions/21825157/…
-
xorcus about 9 yearshere's the best solution I've found: stackoverflow.com/a/20201867/2047385 if (Object.hasOwnProperty.call(window, "ActiveXObject") && !window.ActiveXObject) { // is IE11 }
-
JoshYates1980 over 7 yearsanother option: gist.github.com/CodeHeight/3ad11c1daf8907a53506b869a2251119.js
-
-
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 over 10 yearsI 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 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 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 over 10 yearsThis works for me:
var isIE11 = !!navigator.userAgent.match(/Trident\/7\./);
source -
eivers88 over 10 yearsI 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 over 10 yearsBest 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 about 10 yearsOmu - which version of Chrome? Both lines return false for me with Chrome 35.0.1916.114 m
-
User about 10 yearsWrong code because Acoo browser uses "MSIE" in the useragent. Look at useragentstring.com/pages/Acoo%20Browser
-
Jan. about 10 years@Omu just tested in chrome 35.0.1916.114 on linux and it returns ''false''
-
Alex W about 10 yearsSome 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 about 10 yearsThis seems to me to be more robust. Certainly anything based on user agent is pretty useless.
-
Krunal about 10 yearsWRONG ARGUMENT. I have tested across all IE browsers, even Win8 devices as well.
-
User about 10 yearsYou 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 almost 10 yearsCan you please give a better solution for this?
-
User almost 10 yearsU 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 almost 10 yearsJust tested it. Works perfectly. I think
'ActiveXObject' in window
is good enough for detecting any IE version. -
Eadel almost 10 yearsNice approach, but doesn't work if you select other than default
User Agent String
inEmulation
tab of IE11, leaving document mode default (Edge). -
Alan almost 10 yearsThis Microsoft article suggests this solution may no longer work msdn.microsoft.com/en-us/library/ie/dn423948(v=vs.85).aspx
-
mcw almost 10 yearsActually, this article describes the reason why my method works. Attempting to access
window.ActiveXObject
, as described in the article, returnsundefined
in IE11 now (as well as non-Microsoft browsers.) The test using the javascriptin
operator returnstrue
in all Microsoft browsers, so both are the case strictly in IE11. If Microsoft issues a change to the behavior of thein
operator, yes, this method will break. -
Tarık Özgün Güner over 9 yearsThat's worked instead of ActiveXObject. Thanks a lot
-
Chemical Programmer about 9 yearsIt also returns trun on IE 10 (Win 7)
-
xorcus about 9 yearshere's the best solution I've found: stackoverflow.com/a/20201867/2047385 if (Object.hasOwnProperty.call(window, "ActiveXObject") && !window.ActiveXObject) { // is IE11 }
-
Paul Sweatte almost 9 years@tdakhla Updated to filter out IE Edge.
-
Neo almost 9 years"ActiveXObject" in window returns False in Edge.
-
omer almost 9 yearsthe 'Get IE Version from the User-Agent' work nicely! just to be sure, this will show all version including IE11?
-
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 over 8 yearsThis works great for embeded PDF objects as well when Internet Explorer did their changes.
-
Robin over 8 yearswhy !! symbol is used
-
Jake Rowsell over 8 yearsIf 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 over 8 yearsUsing 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 about 8 years@Neo Edge is not IE, the OP's question was how to detect IE11
-
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 about 8 yearsMy favorite - accounts for IE6-10 and IE11. I added in a check for edge as well
-
Jon Davis almost 8 yearsThis answer should be removed. This was working but lately it has been literally crashing IE for many users who are updating IE11.
-
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 almost 8 yearsIt 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 almost 8 yearsWhere can I find the new standard global objects added to new versions of Edge? I infer Math.acosh is one of those.
-
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).aspx
-
Shaun almost 8 yearsImportant 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 over 7 yearsThis 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 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 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 about 7 yearsJust 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 about 7 yearsThis 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 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 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 about 7 yearsYes, 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 over 6 yearsI 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 about 6 yearsThis detects Firefox as
This is IE 0
-
jackhowa over 4 yearsThis is working for me. Have a basic helper
const isIE11 = () => !!window.MSInputMethodContext && !!document.documentMode;