dean.edwards.name/weblog/2007/03/sniff/


Sniff!

My current frog exaggerator of choice:

var isMSIE = /*@cc_on!@*/false;

Leave a comment »

Comments (57)

  1. It seems to be the fastest and shortest way to know that, thank You!

  2. ooops, maybe this one is even more horrible :D

    var isMSIE = /*@cc_on!@*/!1;

    reduced by 3 chars :rotfl:

  3. [...] Dean Edwards nous propose la méthode la plus courte (en terme de code )que j’ai eu l’occasion de voir. [...]
  4. @Andrea - your solution is actually one extra byte after going through packer (assuming you use false somewhere else in your script).

    • Comment by: -dean
    • Posted: 2007/03/29 4:20 pm
  5. var isMSIE = /*@cc_on!@*/0;

    4 chars ;-)

    • Comment by: Boris
    • Posted: 2007/03/29 4:21 pm
  6. Well if we’re having a competition:

    var isMSIE/*@cc_on=1@*/;
    
    • Comment by: -dean
    • Posted: 2007/03/29 4:28 pm
  7. m/*@cc_on=1@*/

    Hmm, maybe we better just admin Dean already won.

    • Comment by: Doekman
    • Posted: 2007/03/29 4:59 pm
  8. var isMSIE//@cc_on=1

    is a touch shorter but rather more suspect since it takes advantage of the stupid optional-semicolon parsing rules…

    • Comment by: sil
    • Posted: 2007/03/29 5:21 pm
  9. @sil - curse you! I didn’t know that IE supported single line conditional comments. Now I have to go refactor packer…

    • Comment by: -dean
    • Posted: 2007/03/29 5:44 pm
  10. Dean: can’t guarantee that it works in all IEs, just in the one on my desktop…

    • Comment by: sil
    • Posted: 2007/03/29 5:52 pm
  11. come on guys, I didn’t change original result, my solution mantains exactly Dean proposal, a Boolean constructor for isMSIE variable so it’s a JSON “true” or “false” string, not “0″ or “1″ one :D (ok, it’s not so important and I’m joking)

    @sil … I need to update my Cruncher.class.php too (damn, I didn’t know single line comment did it!), however the stupid optional-semicolon is the most important char for an inline (crunched) script ;)

  12. ie5/mac ignores conditional comments, yes?

    • Comment by: Eric Gerds
    • Posted: 2007/03/29 10:00 pm
  13. This is the first time I see this in a single line comment. Cool!

    • Comment by: Kevinin
    • Posted: 2007/03/29 10:18 pm
  14. Eric, nope.

    Dean, it surprises me this works inside an eval() statement!

  15. Hold on, goshdarnit, I haven’t *tested* that across IE versions. Someone needs to test the single-line-comment version before everyone rewrites all the code in the world to use it ;)

    • Comment by: sil
    • Posted: 2007/03/29 11:34 pm
  16. sil, it seems to work correctly with my IE 4, 5, 5.5, 6 (stand alone versions) and 7 too.

    It’s cool enought, now I could do strange things like this one:

    if(1)//@cc_on;/*

    alert("Hello !IE browser");

    //*/

    the way to use IE conditional comments not for IE but for every other browsers, lol :D

  17. .. ehr, probably last comment should be a cc too (for cruncher or parser rules)

    if(1)//@cc_on;/*

    alert("Hello !IE browser");

    //@cc_on*/

    alert("Hello IE too");

    bye and thank you :)

  18. Got one of those for isStrict?

    • Comment by: Doug Hendricks
    • Posted: 2007/03/30 12:36 am
  19. … ehr (again) … I’m not spamming, I’m just sleeping, sorry :D

    //@cc_on/*

    alert("Hello !IE browser");

    //@cc_on*/

    alert("Hello IE too");

  20. You could always cut off the ‘is’ from the beginning.

    • Comment by: Dannii
    • Posted: 2007/03/30 3:06 am
  21. damn it! i lost this competiotion, but if we have to sniff why don’t do it heavily???

    var IEVersion=/*@cc_on function(){ switch(@_jscript_version){ case 1.0:return 3; case 3.0:return 4; case 5.0:return 5; case 5.1:return 5; case 5.5:return 5.5; case 5.6:return 6; case 5.7:return 7; }}()||@*/0;

    minor version are ignored..and i don’t know about mac version ..

  22. @kentaromiura - or you could do this:

    var IEVersion = 0 /*@cc_on+ScriptEngineMajorVersion()@*/;
    • Comment by: -dean
    • Posted: 2007/03/30 5:45 pm
  23. so for the record, this is best, yeah?

    m//@cc_on=1

  24. @Doug -

    var isStrict = document.compatMode == "CSS1Compat";
    
    • Comment by: -dean
    • Posted: 2007/03/30 8:31 pm
  25. @Dustin - Yep. You win a cuddly pink teddy bear. You can call him “Pinkie”.

    • Comment by: -dean
    • Posted: 2007/03/31 6:56 pm
  26. @Dean- [grin] was hoping for 6 chars or less.

    • Comment by: Doug Hendricks
    • Posted: 2007/04/01 3:53 am
  27. [...] Dean also had some fun sniffing browsers and came up with the short test. [...]
  28. I expect my pink teddy bear in the mail. thank you.

  29. Is it alright if I have no idea what you people are talking about?

  30. My Version:

    var isMSIE = true; isMSIE = /*@cc_on!@*/false;

    • Comment by: Andi
    • Posted: 2007/04/03 1:56 am
  31. My version:

    var isMSIE = /msie/.test(navigator.userAgent.toLowerCase())

    You guys just make it such hard work…

    :)

  32. [...] Dean Edwards gives us a nice lean way, using Javascript conditional compilation, to detect IE: var isMSIE = /*@cc_on!@*/false; [...]
  33. This is just great! I will use it from now on. :)

  34. Alistair Potts: Are you joking? Opera used to introduce itself as MSIE.

    And Andi’s comment seems after the whole discussion as spam too.

    • Comment by: Cezary Okupski
    • Posted: 2007/04/04 6:50 pm
  35. my preference going forward has been this:

    var jscript/*@cc_on=@_jscript_version@*/;

    then you can code upon it as

    if (self.jscript) // IE
    else if (self.jscript >= 5.5) // IE 5.5 +
    else if (self.jscript == 5.7) // IE 7.0 Vista
    
    • Comment by: Lucky
    • Posted: 2007/04/04 10:05 pm
  36. @Lucky - brilliant!

    • Comment by: -dean
    • Posted: 2007/04/04 10:11 pm
  37. [...] Dean Edwards придумав один з найлаконічніших способів ідентифікації Internet Explorer’а в JavaScript: Sniff! [...]
  38. [...] Самый короткий способ определения Internet Explorer на JavaScript предложил в своем блоге Dean Edwards: [...]
  39. [...] Sniff! - JavaScript IE detector m//@cc_on=1 (tags: JavaScript Conditional Detectors) [...]
  40. @Dean - Thanks, just want to give back for all the great code/innovations you’ve brought forth.

    • Comment by: Lucky
    • Posted: 2007/04/08 5:03 am
  41. I prefer var isIE = !!document.protocol; because I can easily memorize this formula… ;)

    • Comment by: Wiktor
    • Posted: 2007/04/18 2:59 pm
  42. I found best code with 14(13) symbols.

    IE=0//@cc_on+1
    
    alert(IE)

    More shorts (by sil or Dustin Diaz (11)) require self.IE in condition.

    • Comment by: 12345c
    • Posted: 2007/04/26 4:46 pm
  43. @Dean: Thanks! I didn’t realize you could use conditional compilation to just hide a “!” from other browsers. I had been using this: /*@cc_on @if(@_jscript) var isIE=true; @else */ var isIE=false; /*@end @*/

    To respond to Eric Gerds’ question: IE5/Mac does not support conditional comments, but these are conditional compilations, not conditional comments. Conditional compilation is a feature of JScript which IE5/Mac uses, so it does support this.

    • Comment by: Kravvitz
    • Posted: 2007/05/04 9:44 pm
  44. @Dean - Thanks! I just wanted to let you know that I am thankful for all the great innovations and idea you’ve displayed in your blog.

  45. [...] Simple IE detection in JavaScript (var isMSIE = /*@cc_on!@*/false; and more in the comments) [...]
  46. Nice and elegant method, and very short too ;)

    • Comment by: henri
    • Posted: 2007/09/14 5:52 pm
  47. [...] Depois de familiarizarmos com a sintaxe, podemos escrever códigos como o que Dean Edwards postou aqui: [...]
  48. Looking at how simple this is makes me feel warm inside.

    • Comment by: Lachlan
    • Posted: 2007/09/18 6:19 am
  49. I’ve just realized that “snif” is a worldwide expression :D

    [Alex from France]

    • Comment by: Alex
    • Posted: 2007/09/29 4:50 am
  50. One more code snippet found: arguments and DOM collections can be treated like native Array.
    Examples:

    function bind_params(fn){
        var args=[].slice.call(arguments,1);
        return function(){ fn.call(window,args); }
    }
    
    [].slice.call(document.getElementsByTagName('*'),20);
    

    It is easier to write this way and should work faster.

    • Comment by: DPP
    • Posted: 2007/10/22 7:23 am
  51. [...]     var jscript = NaN/*@cc_on||@_jscript_version@*/; // http://dean.edwards.name/weblog/2007/03/sniff/#comment85164 [...]
  52. hi very thanks

    • Comment by: kompak
    • Posted: 2008/01/23 2:24 pm
  53. [...] simple way to check if the user is having the Internet Explorer web browser, suggested by Dean Edwards (lots of geeky comments here), is to do it like [...]
  54. For the poor people reading this in 2008: The comment in comment #35

    else if (self.jscript == 5.7) // IE 7.0 Vista

    should actually read

    else if (self.jscript == 5.7) // IE 6.0 XP SP3

    Somebody told me “I told you so” in the neighbourhood of 2006 :-)

    (of course there’s no problem if you really just use this to work around JScript version bugs)

    • Comment by: eric
    • Posted: 2008/10/14 1:31 am
  55. [...] une des manières les plus courtes pour savoir si le navigateur est Internet Explorer avec var isMSIE = /@cc_on!@/false; ou plus court encore var isMSIE = [...]
  56. [...] ); O puedes usar otro método más reciente de Dean Edrwards [...]

Leave A Comment

Line and paragraph breaks automatic, email address never displayed.

Help

required

required

required