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 by: eric
Posted: 2008/10/14 1:31 am
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: #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.
Comment: #4.
@Andrea - your solution is actually one extra byte after going through packer (assuming you use
falsesomewhere 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=1Comment: #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.
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.
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.
Comment: #38.
Comment: #39.
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.
Comment: #46.
Nice and elegant method, and very short too
Comment: #47.
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:
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: #51.
Comment: #52.
hi very thanks
Comment: #53.
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.
Comment: #56.
Comment: #57.