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

Sniff!

My current frog exaggerator of choice:

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

Comments (75)

Leave a comment

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

ooops, maybe this one is even more horrible :D

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

reduced by 3 chars :rotfl:

[...] Dean Edwards nous propose la méthode la plus courte (en terme de code )que j’ai eu l’occasion de voir. [...]

@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:
var isMSIE = /*@cc_on!@*/0;

4 chars ;-)

  • Comment by: Boris
  • Posted:

Well if we’re having a competition:

var isMSIE/*@cc_on=1@*/;
  • Comment by: -dean
  • Posted:

m/*@cc_on=1@*/

Hmm, maybe we better just admin Dean already won.

  • Comment by: Doekman
  • Posted:
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:

@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:

Dean: can’t guarantee that it works in all IEs, just in the one on my desktop…

  • Comment by: sil
  • Posted:

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 ;)

ie5/mac ignores conditional comments, yes?

  • Comment by: Eric Gerds
  • Posted:

This is the first time I see this in a single line comment. Cool!

  • Comment by: Kevinin
  • Posted:

Eric, nope.

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

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:

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

.. 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 :)

Got one of those for isStrict?

  • Comment by: Doug Hendricks
  • Posted:

… ehr (again) … I’m not spamming, I’m just sleeping, sorry :D

//@cc_on/*

alert("Hello !IE browser");

//@cc_on*/

alert("Hello IE too");

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

  • Comment by: Dannii
  • Posted:

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 ..

@kentaromiura – or you could do this:

var IEVersion = 0 /*@cc_on+ScriptEngineMajorVersion()@*/;
  • Comment by: -dean
  • Posted:

so for the record, this is best, yeah?

m//@cc_on=1

@Doug -

var isStrict = document.compatMode == "CSS1Compat";
  • Comment by: -dean
  • Posted:

@Dustin – Yep. You win a cuddly pink teddy bear. You can call him “Pinkie”.

  • Comment by: -dean
  • Posted:

@Dean- [grin] was hoping for 6 chars or less.

  • Comment by: Doug Hendricks
  • Posted:

[...] Dean also had some fun sniffing browsers and came up with the short test. [...]

I expect my pink teddy bear in the mail. thank you.

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

My Version:

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

  • Comment by: Andi
  • Posted:

My version:

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

You guys just make it such hard work…

:)

[...] Dean Edwards gives us a nice lean way, using Javascript conditional compilation, to detect IE: var isMSIE = /*@cc_on!@*/false; [...]

This is just great! I will use it from now on. :)

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:

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:

@Lucky – brilliant!

  • Comment by: -dean
  • Posted:

[...] Dean Edwards придумав один з найлаконічніших способів ідентифікації Internet Explorer’а в JavaScript: Sniff! [...]

[...] Самый короткий способ определения Internet Explorer на JavaScript предложил в своем блоге Dean Edwards: [...]

[...] Sniff! – JavaScript IE detector m//@cc_on=1 (tags: JavaScript Conditional Detectors) [...]

@Dean – Thanks, just want to give back for all the great code/innovations you’ve brought forth.

  • Comment by: Lucky
  • Posted:

I prefer var isIE = !!document.protocol; because I can easily memorize this formula… ;)

  • Comment by: Wiktor
  • Posted:

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:

@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:

@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.

[...] Simple IE detection in JavaScript (var isMSIE = /*@cc_on!@*/false; and more in the comments) [...]

Nice and elegant method, and very short too ;)

  • Comment by: henri
  • Posted:

[...] Depois de familiarizarmos com a sintaxe, podemos escrever códigos como o que Dean Edwards postou aqui: [...]

Looking at how simple this is makes me feel warm inside.

  • Comment by: Lachlan
  • Posted:

I’ve just realized that “snif” is a worldwide expression :D

[Alex from France]

  • Comment by: Alex
  • Posted:

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:

[...]     var jscript = NaN/*@cc_on||@_jscript_version@*/; // http://dean.edwards.name/weblog/2007/03/sniff/#comment85164 [...]

hi very thanks

  • Comment by: kompak
  • Posted:

[...] 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 [...]

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:

[...] 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 = [...]

[...] ); O puedes usar otro método más reciente de Dean Edrwards [...]

[...] http://dean.edwards.name/weblog/2007/03/sniff/ [...]

@lucky, eric (comments #35/#54) and others: it is not a good idea to get the browser version by checking the version of its scripting engine: they are independent. See point 4 of this comment.

  • Comment by: Marcel Korpel
  • Posted:

Oh, by the way, that post gives a shorter form of your frog exaggerator:

if(!+"\v1") // true only in IE

But, as noted in another comment, you’ll never be sure if another browser will treat a vertical tab the same way as IE. The same is true for conditional compilation, of course.

  • Comment by: Marcel Korpel
  • Posted:

[...] This is the most compact and insane hack I have ever come across for IE-detection (courtesy: http://dean.edwards.name/weblog/2007/03/sniff/ [...]

var isMSIE = ‘\a’===’a';

  • Comment by: Paul B.
  • Posted:

[...] This is the most compact and insane hack I have ever come across for IE-detection (courtesy: http://dean.edwards.name/weblog/2007/03/sniff/ [...]

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.

  • Comment by: Sarkari naukri
  • Posted:

For anyone that uses Google Closure Compiler, this may be of use to you:

var isMSIE = eval("/*@cc_on!@*/!1");
  • Comment by: Chris West
  • Posted:

i don’t understand comment 35 those else ifs will not get executed if the first if is true

  • Comment by: john
  • Posted:

[...] ref [...]

  • Pingback by: Anonymous
  • Posted:

I am using code in MVC 4.0 but below code makes comment in rest of the MVC code. Please guide me. I want to detect IE browser and version of IE.

var isIE = new Function(“return/*@cc_on!@*/!1″)( ); // [Boolean]

  • Comment by: Monal
  • Posted:

Constantly, men possess aspired following a bigger manhood size. In society, a large and heavy penis is recognized as as the actual gift associated with life. Someone is definitely considered since the man of mans if he’s a large penis.

  • Comment by: sparxx rx
  • Posted:

The Hindi channels are known for their high emotional connect with the Indian audience and the typical ‘saas-bahu’ dramas which are themes of most of the television shows.  Other themes and genres which are characteristic of these shows include relationship between the brother and sister, mythology, drama, crime, reality and horror. Aur Pyar Ho Gaya

  • Comment by: Aur Pyar Ho Gaya
  • Posted:

Long gone are the days when Hindi TV shows were only watched by the retired people and housewives. Today, even the youth wait for the clock to tick 9.00pm to watch their favorite TV show. Drama, soap operas, family stories, love stories and romance were always the most popular TV serials, they still are. To add to the taste, today, Cid

  • Comment by: Cid
  • Posted:

Life-long jobs and being employed by one workplace our total working life is often a thing involving past. With his current fast-paced world-wide economy, careers along with industries apparently change along with come along with go instantaneously. lincoln web hosting

It is definitely sad if a put off comes these are generally the same folks who will fight to launch his or her job look for quickly. It doesn’t should be that means! Here are generally three “must do” occupation success pursuits. vet web design

Organization networking is usually important pertaining to career good results. Keeping your current network active might be fun and also beneficial. Everyone has brought colleagues along with managers which may have left the corporation to go forward to other individuals. To continue to be connected simply pick-up the phone to convey hello. software uptodate

A lot of people will certainly not allow his or her children for you to toil hard of their higher education and learning programs along with deny these people recognition. It’s going to be wrong for doing that as a new parent. The good thing you are able to do is to set up for a fantastic graduation ceremony for ones child. eye candy entertainment

On account of our modern-day living standard were creating trap for the next age group; that is to use the pollution were also collaborating to damage forest to make forest involving concrete. In addition to that; constructionist

Leave A Comment

Line and paragraph breaks automatic, email address never displayed. Some HTML allowed.