Detect Internet Explorer 6 in JavaScript
Update: As some of the comments mention, the technique below doesn't work anymore. It's best to use object detection to accomplish what you need, or use conditional comments. But if you need to detect IE6, this should work: /MSIE 6/i.test(navigator.userAgent)
Sometimes you just have to sniff for Internet Explorer 6 (and under) in JavaScript. Using conditional comments is a decent solution, but I don't want them scattered all over my code.
With a bit of help from Dean Edwards, I worked out the following:
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
With just a single conditional comment, you can have a JavaScript variable that you can reuse throughout your code.
(IE6 will be true in Internet Explorer 6 and under, but does anyone really care about IE5 anymore? Thought not.)
You could use this technique to sniff for other things:
// exactly Internet Explorer 7 var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/; // at least Internet Explorer 7 var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;// any Internet Explorer (thanks to Dean) var isMSIE = /*@[email protected]*/false;
Note: browser sniffing is evil but sometimes painfully necessary.
Comments
1 . Robin on September 25th, 2007
Trouble is, MS recently released JScript 5.7 as a download for IE6 users, so this method of detection promptly fails. Annoyingly.
2 . Marc Palau on February 21st, 2008
That conditionals are a good solutions, recently I'm being using this snipped:
function vIE(){return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;}
to get the right version of IE (or -1 for the rest of browsers)
3 . Dennis Hall on June 4th, 2008
Another potential gotchya is that the conditional comment directive will be stripped if using YUI Compressor.
4 . Mario on July 8th, 2008
this is the good value to compare! (<= 5.7)
var IE6 = false /*@cc_on || @_jscript_version <= 5.7 @*/;
5 . Ion Todirel on September 16th, 2008
isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
or the shorter version:
isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
6 . Greg on October 28th, 2008
This technique will likely fail if the JavaScript is compressed, since many/most compressors strip out comments among other things.
7 . Joe Watkins on November 28th, 2008
Here is a little script I wrote.. its PHP but works great for detecting IE6 and offers up an alert to end user for browser upgrade with a link to M$oft upgrade page. Free and easy to use. php, html, css, jQuery I like the methods above also.
Just wanted to offer a method to get some folks off that darn browser in a graceful way without shunning the user, but by offering help in upgrading.
http://www.thatgrafix.com/php_detect/
8 . James King on December 15th, 2008
Ion, some versions of IE7 have msie 6 in their user agent string.
this is what I have been using in the rare circumstance that I need to detect IE6
IE6=(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1)
9 . James Skemp on December 21st, 2008
@James King: But are they using IE 7, or IE 6?
10 . Stephen Marx on January 8th, 2009
I wouldn't recommend using the method mentioned in this article anymore. I have a version of IE6 that reports a JavaScript version of 5.7.
11 . Kenny on January 9th, 2009
I found this and am using to to swap images from PNGs to GIFs for IE less than 7. It seems to be working well from my testing so far. Thanks!
12 . sangam on January 15th, 2009
I am using following snippet as suggested by Ion Todirel:
isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
[ or the shorter version:
isIE6 = /msie|MSIE 6/.test(navigator.userAgent); ]
It is quite useful, in my case, in dealing with the opacity problem in IE6.
13 . Nils on January 30th, 2009
You say browser sniffing is evil, I guess you mean the essence of exploitation is evil in general... This is wrong. Evil is forexample exploitation used in the purpuse of damaging...
14 . James Skemp on January 30th, 2009
A philosopher :) (And one who's technically correct.)
15 . markedone on February 23rd, 2009
Thanks a lot!
16 . Symaxer on March 2nd, 2009
Thanks!
17 . Starover on March 14th, 2009
Ok, lets use it)
18 . Карл on March 16th, 2009
Stephen Marx, why you wouldn't recommend using the method mentioned in this article anymore?
It actuallity now,
19 . Rach Matt on March 25th, 2009
hei, could u give tutorial about /*@ things @*/
20 . Riad Marrakech on May 7th, 2009
Thank @Ion Todirel
21 . Ariunbayar on September 1st, 2009
Good one. Liked the Ion Todirel's way
22 . Andry on November 13rd, 2009
Ion Todirel's solution will not work for IE8 that has an agent info like this:
"4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
As you can see string MSIE 6 is also present here.
I found a better solution on this forum: http://www.sitepoint.com/forums/showthread.php?t=455334
23 . JD on January 27th, 2010
So simple! Thanks! lets use it :)
24 . Ohm on June 5th, 2010
After various test, I found this solution.
var isIE6 = (navigator.userAgent.toLowerCase().substr(25,6)=="msie 6") ? true : false;
Not "stylish" but it work :P :)
25 . Gaby Dilley on July 30th, 2010
I made a handy dandy tool for realistic web developers:
http://code.google.com/p/css-browser-identifier/
26 . silvan on August 18th, 2010
Posting #5: @Ion Todirel Thanks for share this.
27 . fisherwebdev on September 9th, 2010
Just want to post this, because this comes up pretty high in the search engines.
A lot of folks here are still thinking that Ion's solution is the best, but really it is not, because of the problems noted elsewhere in these comments. The *revised* version of Jesse/Dean's code is the most robust.
Again, that is:
You write this within your HTML, before any other scripts:
<!--[if lte IE 6]>
<script type="text/javascript" src="js/ie6.js"></script>
<![endif]-->
And within js/ie6.js you write (note no "var"):
isMSIE6 = true;
This makes isMSIE6 a global variable within the window object. You may then use this global variable in any other script to test for IE6.
This could easily be modified to test for any version of IE. If you are picky about not using a global variable, you could namespace it.
I don't know about the /*@[email protected]*/ part, but that might be a good idea too.
28 . ugniesdebesys on November 4th, 2010
<!--[if IE 6]>
<style ... >
</style>
<![endif]-->
Use above for just IE 6 at a guess.
id also presume "lt" is "Less Than"
and "lte" is "Less Than or Equal to"
29 . Fort Collins Web Design on November 8th, 2010
Unfortunately, until IE6 is dead, we have to continue using these imperfect methods of detecting M$ junk... Maybe all together they would work.
30 . ugniesdebesys on January 1st, 2011
How to add alert to site, if IE6?
31 . Pushpinder Bagga on March 7th, 2011
I found one on the internet
function is_ie6(){
return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
}