• Detect Internet Explorer 6 in JavaScript

    Sep 21 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 = /*@cc_on!@*/false;
    

    Note: browser sniffing is evil but sometimes painfully necessary.

  • Comments

    1. Robin at 10:21am on September 25, 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 at 5:40am on February 21, 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 at 4:41pm on June 4, 2008

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

    4. Mario at 9:44am on July 8, 2008

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

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

    5. Ion Todirel at 10:38am on September 16, 2008

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

    or the shorter version:

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

    6. Greg at 3:05pm on October 28, 2008

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

    7. Joe Watkins at 3:07am on November 28, 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 at 1:39pm on December 15, 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 at 11:44am on December 21, 2008

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

    10. Stephen Marx at 6:39am on January 8, 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 at 1:30pm on January 9, 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 at 10:43pm on January 15, 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 at 4:16am on January 30, 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 at 7:46am on January 30, 2009

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

    15. markedone at 9:40am on February 23, 2009

    Thanks a lot!

    16. Symaxer at 9:18am on March 2, 2009

    Thanks!

    17. Starover at 12:48pm on March 14, 2009

    Ok, lets use it)

    18. Карл at 6:21am on March 16, 2009

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

    19. Rach Matt at 1:09am on March 25, 2009

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

    20. Riad Marrakech at 7:33pm on May 7, 2009

    Thank @Ion Todirel

    21. Ariunbayar at 6:14am on September 1, 2009

    Good one. Liked the Ion Todirel's way

    22. Andry at 3:23am on November 13, 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 at 3:11am on January 27, 2010

    So simple! Thanks! lets use it :)

    24. Ohm at 7:14am on June 5, 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 at 4:19pm on July 30, 2010

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

    26. silvan at 5:13am on August 18, 2010

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

    27. fisherwebdev at 3:12pm on September 9, 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 /*@cc_on!@*/ part, but that might be a good idea too.

    28. ugniesdebesys at 4:23am on November 4, 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 at 5:06am on November 8, 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 at 7:32am on January 1, 2011

    How to add alert to site, if IE6?

    31. Pushpinder Bagga at 4:53am on March 7, 2011

    I found one on the internet

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

    Commenting is now closed. Come find me on Twitter.