What would be your fastest, shortest (best) way to detect browser which is IE and version less than 9 in JavaScript, without using jQuery or any add-on libraries?
-
I'm fully aware of conditionals in the DOM. Only interested in a small best performing JavaScript solution. – bcm Apr 07 '11 at 01:26
-
5Don't forget that *feature detection* is the most reliable thing when you want to use a version-specific feature (However, the feature can exist but be buggy in some version, keep this in mind). If you want to display browser version on the page, use *browser detection*. – Dan Oct 07 '11 at 16:23
-
3I agree Dan, but in truth, it's often not straight-forward and/or easy for everyone to tie a particular difference to a feature (detection). Even if it is, the code may be easier to read when it is like the answer provided (example: ie < 9). – bcm May 08 '14 at 03:33
14 Answers
Javascript
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : undef;
}());
You can then do:
ie < 9
By James Panolsey from here: http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments

- 63,433
- 20
- 141
- 111
-
2just wondering... is all these (div, all) just in memory or is the DOM actually being accessed multiple times to get the version? – bcm Apr 07 '11 at 02:35
-
It is just in memory, the div it creates doesn't actually get added to the DOM. – Mike Lewis Apr 07 '11 at 02:40
-
5
-
13@TimBüthe: The [comma operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator). Essentially it's adding the inner HTML and returning `all[0]` (which is the first `` in the div). As long as the result is "truthy" (an `` was found), it goes up an IE version and continues on. – Brad Christie Oct 25 '13 at 14:54
-
@BradChristie aha, comma operator. Didn't that exists, thanks a lot. – Tim Büthe Oct 26 '13 at 12:39
-
This while syntax gives me a jshint error but I can't figure out how to fix it : `Expected '{' and instead saw ';'.` – Romain Braun Nov 21 '14 at 18:59
-
jshint is not compensating for the while loops lack of code block or a "do{}" before the while statement, this is valid code however. A while loop evaluates the expression giving and if true will execute a code block if giving. Take this simple test as an example: http://jsfiddle.net/eph39L7p/ Also, have a look at expressions statement: http://www.ecma-international.org/ecma-262/5.1/#sec-12.4 to give a better idea of the while loops expression statement. Think of this as short hand similar to if(true) document.write("true"); – Jason Foglia Feb 24 '15 at 18:27
-
1It's worth noting IE [no longer supports conditional comments](https://msdn.microsoft.com/en-gb/library/hh801214(v=vs.85).aspx) IE10+ – daviestar Feb 12 '16 at 18:57
for what it's worth:
if( document.addEventListener ){
alert("you got IE9 or greater");
}
This successfully targets IE 9+ because the addEventListener
method was supported very early on for every major browser but IE. (Chrome, Firefox, Opera, and Safari) MDN Reference. It is supported currently in IE9 and we can expect it to continue to be supported here on out.

- 14,183
- 17
- 67
- 103

- 7,334
- 8
- 52
- 80
-
1
-
3That's the best answer in my opinion, browser detection should be approached by detecting specific functions/ – 7dr3am7 Nov 08 '13 at 11:23
-
-
4I like this answer. It's not browser version detection, it's browser capability detection - which is usually more useful. Detecting a feature like 'addEventListener' will not only separate IE8 from IE9, it will separate old browsers from HTML5 capable browsers in general. – garyv Feb 20 '14 at 17:38
-
8
-
6Unfortunately, this fails if you've polyfilled `document.addEventListener`. IE conditional comments are failproof in that regard. – Daniel Weiner Jan 28 '15 at 17:20
-
2To prevent method failing from polifills. Just turn it arround and check: `if( !document.attachEvent){ // IE8+ }` – Jesper Jensen Jul 02 '15 at 06:12
-
1Yeah this isn't a solution for enterprise work, since polyfills from some other block of code somewhere else could screw up your detection. Might work locally, and then break when it deploys. :/ – dudewad Oct 16 '15 at 17:22
-
So @JesperJensen and OP, if I wanted to turn this around to check if you have ie 8 or below, could I say `if( !document.addEventListener ){ alert("you have IE8 or less"); }` ? – Kyle Vassella Dec 04 '17 at 23:06
Using conditional comments, you can create a script block that will only get executed in IE less than 9.
<!--[if lt IE 9 ]>
<script>
var is_ie_lt9 = true;
</script>
<![endif]-->
Of course, you could precede this block with a universal block that declares var is_ie_lt9=false
, which this would override for IE less than 9. (In that case, you'd want to remove the var
declaration, as it would be repetitive).
EDIT: Here's a version that doesn't rely on in-line script blocks (can be run from an external file), but doesn't use user agent sniffing:
Via @cowboy:
with(document.createElement("b")){id=4;while(innerHTML="<!--[if gt IE "+ ++id+"]>1<![endif]-->",innerHTML>0);var ie=id>5?+id:0}

- 37,023
- 22
- 103
- 153
-
2
-
It has the advantage of not involving any RegEx and, presumably, not being spoofable. IE parses the variable like its nothing. (Which, for IE, is saying something.) – Yahel Apr 07 '11 at 01:30
-
this is pretty good, I could use if(window.ielt9). I wonder if there could there be a non-inline script solution that is better than this? (which would be posh...) – bcm Apr 07 '11 at 01:39
-
It seems like @cwolves's solution would allow you to run it in an external script (I've never tried it.) – Yahel Apr 07 '11 at 01:41
bah to conditional comments! Conditional code all the way!!! (silly IE)
<script type="text/javascript">
/*@cc_on
var IE_LT_9 = (@_jscript_version < 9);
@*/
</script>
Seriously though, just throwing this out there in case it suits you better... they're the same thing, this can just be in a .js file instead of inline HTML
Note: it is entirely coincidental that the jscript_version check is "9" here. Setting it to 8, 7, etc will NOT check "is IE8", you'd need to lookup the jscript versions for those browsers.
-
@cwolves how is this differentiating between IE less than 9 and IE9? seems to be returning the same result? – bcm Apr 07 '11 at 01:43
-
1@bcm - you should get `IE_LT_9 == false` in IE9 and `true` in IE8. I'm using a mac right now and don't have a PC here, so I can't test it, but I pulled that out of code I wrote that I know works. If it's not working for some reason, `alert(@_jscript_version)` to see what you get and adjust from there. – Apr 07 '11 at 01:45
-
@cwolves it is returning 9 regardless of IE browser version on PC. so it's a no-go. – bcm Apr 07 '11 at 01:47
-
2_sigh_ the code works, IE9 doesn't actually run IE7 when it's in 'compatibility mode', it still uses IE9's JS engine. http://jsfiddle.net/aNGXS/ According to IETester: IE9 says `9', IE8 says '5.6' – Apr 07 '11 at 02:00
-
I wouldn't depend on IETester, I just tried and it didn't even give me a result for IE8 on my pc. IE dev tool might be more reliable, or even better, standalone. – bcm Apr 07 '11 at 02:05
-
1either way, use a stand-alone version of IE8 and you WILL NOT get '9' in that alert. You'll get '5.8' or something similar (not sure specifically if they ever updated the JScript engine, but it's NOT 9) – Apr 07 '11 at 02:06
-
1OMFG, I just tested this on FIVE machines via RDC and it works on EVERY one. IE8 says '5.8', IE9 says '9'. You're doing something wrong or assuming that you're not using the IE9 engine when you are. As I said, IE9 in "compatibility mode" or with a different user agent is still IE9. Run a stand-alone version of IE8, IE7 or anything previous and the code works. – Apr 07 '11 at 02:07
-
I'm just switching between BrowserMode+DocumentMode in IE dev toolbar, matching 7-7, 8-8 and 9-9. nothing unusual. – bcm Apr 07 '11 at 02:10
-
3You're still running IE9 though! This detects -THAT-. If you run a standalone copy if IE8, it'll show that. Don't blame me for Microsoft dev tools being worthless. – Apr 07 '11 at 02:13
Below is an improvement over James Padolsey's solution:
1) It doesn't pollute memory (James' snippet creates 7 unremoved document fragments when detecting IE11, for example).
2) It's faster since it checks for a documentMode value before generating markup.
3) It's far more legible, especially to beginning JavaScript programmers.
Gist link: https://gist.github.com/julianshapiro/9098609
/*
- Behavior: For IE8+, we detect the documentMode value provided by Microsoft.
- Behavior: For <IE8, we inject conditional comments until we detect a match.
- Results: In IE, the version is returned. In other browsers, false is returned.
- Tip: To check for a range of IE versions, use if (!IE || IE < MAX_VERSION)...
*/
var IE = (function() {
if (document.documentMode) {
return document.documentMode;
} else {
for (var i = 7; i > 0; i--) {
var div = document.createElement("div");
div.innerHTML = "<!--[if IE " + i + "]><span></span><![endif]-->";
if (div.getElementsByTagName("span").length) {
return i;
}
}
}
return undefined;
})();

- 236
- 1
- 3
- 6
var ie = !-[1,]; // true if IE less than 9
This hack is supported in ie5,6,7,8. It is fixed in ie9+ (so it suits demands of this question). This hack works in all IE compatibility modes.
How it works: ie engine treat array with empty element (like this [,1]) as array with two elements, instead other browsers think that there is only one element. So when we convert this array to number with + operator we do something like that: (',1' in ie / '1' in others)*1 and we get NaN in ie and 1 in others. Than we transform it to boolean and reverse value with !. Simple. By the way we can use shorter version without ! sign, but value will be reversed.
This is the shortest hack by now. And I am the author ;)

- 960
- 1
- 9
- 10
-
Can you explain how / why that works? It looks like a bit of a nasty hack: you're relying on JavaScript engine quirks and that particular versions of the JavaScript engine correspond to particular IE versions. Does that still work in newer IEs set to back compatibility in the debug tools, or compatibility mode? – Rup Jun 03 '15 at 11:52
-
This hack is supported in ie5,6,7,8. It is fixed in ie9+ (so it suits demands of this question). How it works: ie engine treat array with empty element (like this [,1]) as array with two elements, instead other browsers think that there is only one element. So when we convert this array to number with + operator we do something like that: (',1' in ie / '1' in others)*1 and we get NaN in ie and 1 in others. Than we transform it to boolean and reverse value with !. Simple. By the way we can use shorter version without ! sign, but value will be reversed. – Aleko Jun 03 '15 at 14:57
-
OK, but if I put IE 9 into IE 7 compatibility mode will it detect IE 7? Thanks for the explanation, though - that would be better edited into the answer rather than left as a comment. – Rup Jun 03 '15 at 15:02
I've decided to go with object detection instead.
After reading this: http://www.quirksmode.org/js/support.html and this: http://diveintohtml5.ep.io/detect.html#canvas
I'd use something like
if(!!document.createElement('canvas').getContext) alert('what is needed, supported');
This link contains relevant information on detecting versions of Internet Explorer:
http://tanalin.com/en/articles/ie-version-js/
Example:
if (document.all && !document.addEventListener) {
alert('IE8 or older.');
}

- 13,927
- 1
- 36
- 52

- 4,317
- 2
- 23
- 15
-
I like your answer best. Simple, not many lines of code and flexible. – user2662680 Dec 29 '15 at 16:06
You could do it in a quick and dirty fashion with a regular expression and .match()
:
if (navigator.userAgent.match(/MSIE\s(?!9.0)/)) {
// ie less than version 9
}

- 64,178
- 48
- 151
- 180
-
8Unfortunately that only detects IEs other than IE 9. For example it will also be true for IE 10. – Tim Jansen Sep 26 '12 at 08:20
If I were you I would use conditional compilation or feature detection.
Here's another alternative:
<!--[if lt IE 9]><!-->
<script>
var LTEIE8 = true;
</script>
<!--<![endif]-->

- 14,806
- 5
- 56
- 89
I liked Mike Lewis' answer but the code did not pass jslint and I could not understand the funky while loop. My use case is to put up a browser not supported message if less than or equal to IE8.
Here is a jslint free version based on Mike Lewis':
/*jslint browser: true */
/*global jQuery */
(function () {
"use strict";
var browserNotSupported = (function () {
var div = document.createElement('DIV');
// http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx
div.innerHTML = '<!--[if lte IE 8]><I></I><![endif]-->';
return div.getElementsByTagName('I').length > 0;
}());
if (browserNotSupported) {
jQuery("html").addClass("browserNotSupported").data("browserNotSupported", browserNotSupported);
}
}());

- 2,242
- 2
- 20
- 16
-
1because [jquery 2.x doesn't support ie < 9](http://jquery.com/browser-support/) so that piece of code would result in errors and crash and fear and doom. – Feb 06 '14 at 07:05
if (+(/MSIE\s(\d+)/.exec(navigator.userAgent)||0)[1] < 9) {
// IE8 or less
}
- extract IE version with:
/MSIE\s(\d+)/.exec(navigator.userAgent)
- if it's non-IE browser this will return
null
so in that case||0
will switch thatnull
to0
[1]
will get major version of IE orundefined
if it was not an IE browser- leading
+
will convert it into a number,undefined
will be converted toNaN
- comparing
NaN
with a number will always returnfalse

- 186
- 1
- 4
Does it need to be done in JavaScript?
If not then you can use the IE-specific conditional comment syntax:
<!--[if lt IE 9]><h1>Using IE 8 or lower</h1><![endif]-->

- 263,068
- 57
- 365
- 409
You are all trying to overcomplicate such simple things. Just use a plain and simple JScript conditional comment. It is the fastest because it adds zero code to non-IE browsers for the detection, and it has compatibility dating back to versions of IE before HTML conditional comments were supported. In short,
var IE_version=(-1/*@cc_on,@_jscript_version@*/);
Beware of minifiers: most (if not all) will mistake the special conditional comment for a regular comment, and remove it
Basically, then above code sets the value of IE_version to the version of IE you are using, or -1 f you are not using IE. A live demonstration:
var IE_version=(-1/*@cc_on,@_jscript_version@*/);
if (IE_version!==-1){
document.write("<h1>You are using Internet Explorer " + IE_version + "</h1>");
} else {
document.write("<h1>You are not using a version of Internet Explorer less than 11</h1>");
}