Internet Explorer 11 detection
I have tried to detect IE 11 with answer specified for this question’ Jquery fail to detect IE 11 Thats !!navigator.userAgent.match(/Trident\/7\./) But I am getting error Object not found and needs to be re-evaluated. Then I openede developer console in IE11 and tried to access some predefined javascript objects, I am still getting same error. I have tried navigator.userAgent window.navigator console.log(‘test’); Anyone have any idea about it ?
@Bobkhin I have mentioned above my issue. getting error Object not found and needs to be re-evaluated.
12 Answers 12
Edit 18 Nov 2016
This code also work (for those who prefer another solution , without using ActiveX)
var isIE11 = !!window.MSInputMethodContext && !!document.documentMode; // true on IE11 // false on Edge and other IEs/browsers.
Original Answer
In order to check Ie11 , you can use this : ( tested)
!(window.ActiveXObject) && «ActiveXObject» in window
Notice : this wont work for IE11 :
as you can see here , it returns true :
Apparently , they added the machine bit space :
"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"
"Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"
/x64|x32/ig.test(window.navigator.userAgent)
this will return true only for ie11.
@Jan. yes. only IE11 would return true for this whole condition. I just showed that
@IanSteffy This is just to show the result(!) of the operation. You can open a HTML file and add script tag and run the command inside that script. Here run this.
Wait, there’s an IE12? I thought IE11 was the last one and that we soon won’t have to worry about that stupid browser anymore! ☹
Does not work for my IE 11.0.9600.19431 on Windows 7 Enterprise. There is no window.MSInputMethodContext .
To detect MSIE (from version 6 to 11) quickly:
if(navigator.userAgent.indexOf('MSIE')!==-1 || navigator.appVersion.indexOf('Trident/') > -1) < /* Microsoft Internet Explorer detected in. */ >
Yeah, I don’t know why it’s >0 and not >-1, but «Trident/» shows up much farther into the appVersion string anyways-
I use the following function to detect version 9, 10 and 11 of IE:
function ieVersion() < var ua = window.navigator.userAgent; if (ua.indexOf("Trident/7.0") >-1) return 11; else if (ua.indexOf("Trident/6.0") > -1) return 10; else if (ua.indexOf("Trident/5.0") > -1) return 9; else return 0; // not IE9, 10 or 11 >
All of the above answers ignore the fact that you mention you have no window or navigator 🙂
Then I openede developer console in IE11
Object not found and needs to be re-evaluated.
and navigator, window, console, none of them exist and need to be re-evaluated. I’ve had that in emulation. just close and open the console a few times.
goodness gracious THANK YOU for actually reading the question and answering the problem with re-evaluation.
Closing and opening the console worked for me. Strange behavior compared to Firefox or Chrome (but it’s IE, so that figures.)
A pretty safe & concise way to detect IE 11 only is
msCrypto is a prefixed version of the window.crypto object and only implemented in IE 11.
https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
The only working solution (that’s the shortest, cleanest and the most elegant one)! Royi Namir’s /x64|x32/ig.test(window.navigator.userAgent) applies to Firefox too!
Okay try this, simple and for IE11 and IE below 11 version
browserIsIE = navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 || navigator.userAgent.toUpperCase().indexOf("MSIE") != -1;
navigator.userAgent.toUpperCase().indexOf(«TRIDENT/») != -1 for IE 11 version navigator.userAgent.toUpperCase().indexOf(«MSIE») != -1 for IE below 11 version
browserIsIE = navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 || navigator.userAgent.toUpperCase().indexOf("MSIE") != -1; console.log('Is IE Browser : '+ browserIsIE)
And how I implemented this
I think you have a typo in your function. First, you do the condition check, which is not used. Second, perhaps you mean && «ActiveXObject» in window . Third: what the trick with double negation !! ?
This link was helpful . It contains the javascript code to detect all versions of IE up to IE11. I tested the script with IE11 emulator. To find the IE11 emulator, right-click on the web browser click «Inspect element». At the bottom-left of the page, scroll down the navigation bar and click the desktop icon. The «User Agent String» dropdown box contains options to emulate IE6-11.
It works. I just used it some minutes before writing this answer. Cannot post snapshots — not enough reputation.
This is the code — follow the link to view it again:
// Get IE or Edge browser version var version = detectIE(); if (version === false) < document.getElementById('result').innerHTML = 'IE/Edge'; > else if (version >= 12) < document.getElementById('result').innerHTML = 'Edge ' + version; >else < document.getElementById('result').innerHTML = 'IE ' + version; >// add details to debug result document.getElementById('details').innerHTML = window.navigator.userAgent; /** * detect IE * returns version of IE or false, if browser is not Internet Explorer */ function detectIE() < var ua = window.navigator.userAgent; // Test values; Uncomment to check result … // IE 10 // ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'; // IE 11 // ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'; // Edge 12 (Spartan) // ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0'; // Edge 13 // ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586'; var msie = ua.indexOf('MSIE '); if (msie >0) < // IE 10 or older =>return version number return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10); > var trident = ua.indexOf('Trident/'); if (trident > 0) < // IE 11 =>return version number var rv = ua.indexOf('rv:'); return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10); > var edge = ua.indexOf('Edge/'); if (edge > 0) < // Edge (IE 12+) =>return version number return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10); > // other browser return false; >
@import url(https://fonts.googleapis.com/css?family=Fira+Mono|Fira+Sans:300); body < color: black; background-color: white; font-family: "Fira Sans", sans-serif; font-weight: 300; margin: 0; padding: 3rem; >h1 < color: darkgrey; text-align: center; font-weight: 300; font-size: 1.5rem; line-height: 2rem; >h2 < text-align: center; font-weight: 300; font-size: 4rem; >p
Detect IE/Edge version with JavaScript.
Updated to recognize Internet Explorer 12+ aka Edge.
detecting…
n/a
If Browser is Internet Explorer: run an alternative script instead
I’m using an image carousel script that is quite heavy on the browser. It works great in Opera and Chrome, half decent in FF and absolutely breaks my balls in IE. So i’d like to give IE users an alternative of simple HTML without any action/JS. The script doesn’t use MT or jQuery and its like 380 lines of JS. Would it be possible to give IE users a plain HTML alternative? var browserName=navigator.appName; if (browserName==»Microsoft Internet Explorer») < // what command can i use? >
Mind that all (or almost) the answers below mention conditional comments. Conditional comments have been deprecated since IE10. See blogs.msdn.com/b/ie/archive/2011/07/06/…
Other people come to the questions posted here for answers, sometimes many years after the question was initially asked Victor. @AdrienBe is politely letting others know that the answers with conditional comments are no longer viable. Downvotes on deprecated answers are helpful to people coming to the thread to learn what to do for this problem. Votes let others know what the community thinks is the best answer, and what answers should not be tried. Try to not appear so wounded by the process.
10 Answers 10
If your JS is unobtrusive, you can just use:
@AdrienBe Were they trying to kill off Internet Explorer? Why remove the feature that allows us to accommodate its flaws?
You can do something like this to include IE-specific javascript:
Just noting that this only works on IE9 and below. Since the only version of IE that is in any use at all in 2020 is IE 11 this method is no longer viable.
You are correct — see: stackoverflow.com/a/22187600/65295. Hopefully nobody is still developing specifically for IE 🙂
For IE10+ standard conditions don’t work cause of engine change or some another reasons, cause, you know, it’s MSIE. But for IE10+ you need to run something like this in your scripts:
if (navigator.userAgent.match(/Trident\/7\./)) < // do stuff for IE. >
You don’t actually need the double bang !! at the start for it to automatically convert itself into a Boolean.
Also, the test method is going to be faster than the match method. Normally such micro-optimizations would not be important but since you are trying to initiate an alternate script during page load, optimization does matter in this instance.
@StephenMIrving double bang is kinda semantics. Let’s say if you’re gonna be writing in typescript it will show you an error of wrong type being passed into if clause. So basically I do the type transformation manually. Also I’d say that test will be a better match here though also mostly for semantic purposes as optimizing microseconds here won’t give you a lot of profit. This won’t be running multiple times, so noone will notice such performance change.
I have to disagree with you strongly on both counts. The double bang is useless smelly fluff that provides no value. If there is a purpose for it in Typescript that is irrelevant as we aren»t discussing Typescript code, this is JS and JS has implicit type coercion as a feature. There is no point to doing the type coercion manually here. Second, as I explained in my original answer, this code is being run while the page loads and is going to branch the scripting. Every ms that can be shaved off initial load time and the TTCP is valuable, and for companies that value is quantifiable in dollars.
JavaScript: The best way to detect IE
You can check for Trident, IE’s engine, by the following:
var trident = !!window.ActiveXObject;
As stated on MSDN it is only supported in IE.
Note: above code returns false in IE-11, If you want to detect also IE-11 use this:
var isIE = "ActiveXObject" in window; //window.ActiveXObject !== undefined;
this is THE answer. i’m impressed. short and to the point. feature detection for IE10 is problematic, because it seems to be a damn fine leap forward in everything.
If you can avoid it, don’t test for browsers. Do feature detection. This will mean that your code is (more likely to be) future-proof. In this case, for instance, if you discovered that the browser was IE and decided to use attachEvent because of it, you would miss out on the fact that addEventListener (superior) is available in IE9.
In this case, test to see if document.addEventListener exists. If it does, you have the answer.
if (document.addEventListener) < document.addEventListener(. ); >else
Edit: duri’s comment above shows that this test fails in IE9 (as per standards), which actually means it is a perfect test for addEventListener , since that is available from IE9. However it is still far, far better to program for specific functionality, rather than specific browsers.
In 99.999999% of cases this is the best advice there is on the subject. But there are very rare instances when you actually may need to detect IE itself rather than particular features/functionality.
The question uses an example peripherally — no need to critique it. Strangely, ievv is a form of feature detection (albeit not a fine grained one.)
Q: shortest ie detection ever? A: sure seems to be. (also, it «detects» the handling of vertical-tabs)
This would be great if IE 11 didn’t lie about a load of features to try and make sites treat it like Firefox or chrome when it simply doesn’t work well enough to be treated as a standard browser yet(and it’s debatable if it ever will, since the browser has resorted to lying). ActiveXObject is falsey, the user agent has been changed, and they removed conditional comments.
To check if the browser is Internet Explorer, use feature detection to check for documentMode :
This code checks to see if the browser is Internet Explorer 8, 9, 10, or 11:
var docMode = document.documentMode, hasDocumentMode = (docMode !== undefined), isIE8 = (docMode === 8), isIE9 = (docMode === 9), isIE10 = (docMode === 10), isIE11 = (docMode === 11), isMsEdge = window.navigator.userAgent.indexOf("Edge/") > -1; // browser is IE if(hasDocumentMode) < if(isIE11)< // browser is IE11 >else if(isIE10) < // browser is IE10 >else if(isIE9) < // browser is IE9 >else if(isIE8) < // browser is IE8 >> else < // document.documentMode is deprecated in MS Edge if(isMsEdge)< // browser is MS Edge >>
Checking document.documentMode will only work in IE8 through IE11, since documentMode was added in IE8 and has been deprecated / removed in MS Edge.
If you really need to detect IE7, check for document.attachEvent :
var isIE7 = (document.attachEvent !== undefined); if(isIE7) < // browser is IE7 >
IE7 returns a object, but if the browser is IE11 (for example), then this would come back as undefined , since IE11 does not have attachEvent .
Added check for MS Edge. document.documentMode was deprecated in MS Edge. Due to the nature of MS Edge, you can check for Edge/ in the User Agent. Microsoft is making it difficult to use feature detection in MS Edge.