the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • Detect Internet Explorer 6 in JavaScript

    Sep 22 2007

    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.

    Tags: javascript ie6 browsersniff
    View 2 Comments | Add a comment
  • Comments

    1. Robin at 4:21pm 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 11: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)

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
  • Recent Articles

    • Parse Accept-Language to detect a user's language
    • Twitter
    • Three years of The Future of the Web
    • Saving data to a file with PHP
    • Easy web scraping with PHP
    • See all the articles
    • IBM: Where and when to use Ajax
    • Code Igniter 1.6.0 Released
    • Update a Dev Site Automatically with Subversion
    • JavaScript Functions are Variables
    • See All...
  • Categories

    • javascript (37)
    • links (19)
    • about (18)
    • web (14)
    • server (10)
    • html (10)
    • css (8)
    • carnival (7)
    • browsers (7)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
    • work (4)
  • Older Articles

    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2008 Jesse Skinner | CSS | XHTML | RSS | Help | Impressum | Cutie Quilts | Internet Blog Top Sites