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(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
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
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 ..
@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.
[…] 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: #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 = […]
Comment: #56
[…] ); O puedes usar otro método más reciente de Dean Edrwards […]
@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.
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.
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.
Comments (67)
Leave a comment
Comment: #1
It seems to be the fastest and shortest way to know that, thank You!
Comment: #2
ooops, maybe this one is even more horrible
reduced by 3 chars :rotfl:
Comment: #3
[…] Dean Edwards nous propose la méthode la plus courte (en terme de code )que j’ai eu l’occasion de voir. […]
Comment: #4
@Andrea – your solution is actually one extra byte after going through packer (assuming you use
false
somewhere else in your script).Comment: #5
4 chars
Comment: #6
Well if we’re having a competition:
Comment: #7
m/*@cc_on=1@*/
Hmm, maybe we better just admin Dean already won.
Comment: #8
is a touch shorter but rather more suspect since it takes advantage of the stupid optional-semicolon parsing rules…
Comment: #9
@sil – curse you! I didn’t know that IE supported single line conditional comments. Now I have to go refactor packer…
Comment: #10
Dean: can’t guarantee that it works in all IEs, just in the one on my desktop…
Comment: #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
(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
Comment: #12
ie5/mac ignores conditional comments, yes?
Comment: #13
This is the first time I see this in a single line comment. Cool!
Comment: #14
Eric, nope.
Dean, it surprises me this works inside an eval() statement!
Comment: #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: #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
Comment: #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
Comment: #18
Got one of those for isStrict?
Comment: #19
… ehr (again) … I’m not spamming, I’m just sleeping, sorry
//@cc_on/*
alert("Hello !IE browser");
//@cc_on*/
alert("Hello IE too");
Comment: #20
You could always cut off the ‘is’ from the beginning.
Comment: #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 ..
Comment: #22
@kentaromiura – or you could do this:
Comment: #23
so for the record, this is best, yeah?
m//@cc_on=1
Comment: #24
@Doug –
Comment: #25
@Dustin – Yep. You win a cuddly pink teddy bear. You can call him “Pinkie”.
Comment: #26
@Dean- [grin] was hoping for 6 chars or less.
Comment: #27
[…] Dean also had some fun sniffing browsers and came up with the short test. […]
Comment: #28
I expect my pink teddy bear in the mail. thank you.
Comment: #29
Is it alright if I have no idea what you people are talking about?
Comment: #30
My Version:
var isMSIE = true; isMSIE = /*@cc_on!@*/false;
Comment: #31
My version:
var isMSIE = /msie/.test(navigator.userAgent.toLowerCase())
You guys just make it such hard work…
Comment: #32
[…] Dean Edwards gives us a nice lean way, using Javascript conditional compilation, to detect IE: var isMSIE = /*@cc_on!@*/false; […]
Comment: #33
This is just great! I will use it from now on.
Comment: #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: #35
my preference going forward has been this:
then you can code upon it as
Comment: #36
@Lucky – brilliant!
Comment: #37
[…] Dean Edwards придумав один з найлаконічніших способів ідентифікації Internet Explorer’а в JavaScript: Sniff! […]
Comment: #38
[…] Самый короткий способ определения Internet Explorer на JavaScript предложил в своем блоге Dean Edwards: […]
Comment: #39
[…] Sniff! – JavaScript IE detector m//@cc_on=1 (tags: JavaScript Conditional Detectors) […]
Comment: #40
@Dean – Thanks, just want to give back for all the great code/innovations you’ve brought forth.
Comment: #41
I prefer
var isIE = !!document.protocol;
because I can easily memorize this formula…Comment: #42
I found best code with 14(13) symbols.
More shorts (by sil or Dustin Diaz (11)) require self.IE in condition.
Comment: #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: #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.
Comment: #45
[…] Simple IE detection in JavaScript (var isMSIE = /*@cc_on!@*/false; and more in the comments) […]
Comment: #46
Nice and elegant method, and very short too
Comment: #47
[…] Depois de familiarizarmos com a sintaxe, podemos escrever códigos como o que Dean Edwards postou aqui: […]
Comment: #48
Looking at how simple this is makes me feel warm inside.
Comment: #49
I’ve just realized that “snif” is a worldwide expression
[Alex from France]
Comment: #50
One more code snippet found: arguments and DOM collections can be treated like native Array.
Examples:
It is easier to write this way and should work faster.
Comment: #51
[…] var jscript = NaN/*@cc_on||@_jscript_version@*/; // http://dean.edwards.name/weblog/2007/03/sniff/#comment85164 […]
Comment: #52
hi very thanks
Comment: #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 […]
Comment: #54
For the poor people reading this in 2008: The comment in comment #35
should actually read
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: #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 = […]
Comment: #56
[…] ); O puedes usar otro método más reciente de Dean Edrwards […]
Comment: #57
[…] http://dean.edwards.name/weblog/2007/03/sniff/ […]
Comment: #58
@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: #59
Oh, by the way, that post gives a shorter form of your frog exaggerator:
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: #60
[…] 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/ […]
Comment: #61
var isMSIE = ‘\a’===’a’;
Comment: #62
[…] 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/ […]
Comment: #63
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: #64
For anyone that uses Google Closure Compiler, this may be of use to you:
Comment: #65
i don’t understand comment 35 those else ifs will not get executed if the first if is true
Comment: #66
[…] ref […]
Comment: #67
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]
Comments are closed.