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 = /*@cc_on!@*/false;
Note: browser sniffing is evil but sometimes painfully necessary.
Trouble is, MS recently released JScript 5.7 as a download for IE6 users, so this method of detection promptly fails. Annoyingly.
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)
Another potential gotchya is that the conditional comment directive will be stripped if using YUI Compressor.
this is the good value to compare! (<= 5.7)
var IE6 = false /*@cc_on || @_jscript_version <= 5.7 @*/;
isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
or the shorter version:
isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
This technique will likely fail if the JavaScript is compressed, since many/most compressors strip out comments among other things.
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/
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)
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.
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!
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.
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...
Stephen Marx, why you wouldn't recommend using the method mentioned in this article anymore?
It actuallity now,
hei, could u give tutorial about /*@ things @*/
Good one. Liked the Ion Todirel's way
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
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 :)
I made a handy dandy tool for realistic web developers:
http://code.google.com/p/css-browser-identifier/
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 /*@cc_on!@*/ part, but that might be a good idea too.
<!--[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"
Unfortunately, until IE6 is dead, we have to continue using these imperfect methods of detecting M$ junk... Maybe all together they would work.
I found one on the internet
function is_ie6(){
return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
}