Coding with Jesse

Detect Internet Explorer 6 in JavaScript

September 21st, 2007

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.


Interested in web development? Subscribe to my newsletter!

Comments

1 . Robin at 2007-09-25T14:21:52.000Z

Robin

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 at 2008-02-21T10:40:01.000Z

Marc Palau

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 at 2008-06-04T20:41:35.000Z

Dennis Hall

Another potential gotchya is that the conditional comment directive will be stripped if using YUI Compressor.

4 . Mario at 2008-07-08T13:44:24.000Z

Mario

this is the good value to compare! (<= 5.7)

var IE6 = false /*@cc_on || @_jscript_version <= 5.7 @*/;

5 . Ion Todirel at 2008-09-16T14:38:12.000Z

Ion Todirel

isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;

or the shorter version:

isIE6 = /msie|MSIE 6/.test(navigator.userAgent);

6 . Greg at 2008-10-28T19:05:26.000Z

Greg

This technique will likely fail if the JavaScript is compressed, since many/most compressors strip out comments among other things.

7 . Joe Watkins at 2008-11-28T08:07:05.000Z

Joe Watkins

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 at 2008-12-15T18:39:14.000Z

James King

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 at 2008-12-21T16:44:16.000Z

James Skemp

@James King: But are they using IE 7, or IE 6?

10 . Stephen Marx at 2009-01-08T11:39:17.000Z

Stephen Marx

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 at 2009-01-09T18:30:17.000Z

Kenny

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 at 2009-01-16T03:43:18.000Z

sangam

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 at 2009-01-30T09:16:31.000Z

Nils

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 at 2009-01-30T12:46:46.000Z

James Skemp

A philosopher :) (And one who's technically correct.)

15 . markedone at 2009-02-23T14:40:52.000Z

markedone

Thanks a lot!

16 . Symaxer at 2009-03-02T14:18:36.000Z

Symaxer

Thanks!

17 . Starover at 2009-03-14T16:48:59.000Z

Starover

Ok, lets use it)

18 . Карл at 2009-03-16T10:21:32.000Z

Карл

Stephen Marx, why you wouldn't recommend using the method mentioned in this article anymore?
It actuallity now,

19 . Rach Matt at 2009-03-25T05:09:43.000Z

Rach Matt

hei, could u give tutorial about /*@ things @*/

20 . Riad Marrakech at 2009-05-07T23:33:02.000Z

Riad Marrakech

Thank @Ion Todirel

21 . Ariunbayar at 2009-09-01T10:14:38.000Z

Ariunbayar

Good one. Liked the Ion Todirel's way

22 . Andry at 2009-11-13T08:23:17.000Z

Andry

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 at 2010-01-27T08:11:21.000Z

JD

So simple! Thanks! lets use it :)

24 . Ohm at 2010-06-05T11:14:26.000Z

Ohm

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 at 2010-07-30T20:19:06.000Z

Gaby Dilley

I made a handy dandy tool for realistic web developers:
http://code.google.com/p/css-browser-identifier/

26 . silvan at 2010-08-18T09:13:24.000Z

silvan

Posting #5: @Ion Todirel Thanks for share this.

27 . fisherwebdev at 2010-09-09T19:12:59.000Z

fisherwebdev

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 at 2010-11-04T08:23:55.000Z

ugniesdebesys

<!--[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 at 2010-11-08T10:06:18.000Z

Fort Collins Web Design

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 at 2011-01-01T12:32:00.000Z

ugniesdebesys

How to add alert to site, if IE6?

31 . Pushpinder Bagga at 2011-03-07T09:53:35.000Z

Pushpinder Bagga

I found one on the internet

function is_ie6(){
return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
}