Detecting IE in 12 bytes
Wednesday, 28 January 2009
Andrea Giammarchi had a interesting article which stated you can detect IE in 32 bytes of code. I wondered if this could be improved, after a few failed attempts I found this to be the smallest and fastest way:-
IE='\v'=='v'
Pretty cool eh?
No. 1 — January 28th, 2009 at 12:41 pm
cute π
No. 2 — January 28th, 2009 at 1:06 pm
Very good!
No. 3 — January 28th, 2009 at 4:16 pm
Sweeet!
No. 4 — January 28th, 2009 at 4:43 pm
detecting everything else…
FF=’\v'<‘v’
actually tested it only in firefox π
No. 5 — January 28th, 2009 at 7:34 pm
Never fail to impress me Gareth. Wish I had your JS skills.
No. 6 — January 29th, 2009 at 5:42 am
Very cool! You can also abbreviate it to:
E=’\v’==’v’
Where E is for explorer π
No. 7 — January 29th, 2009 at 6:39 am
Love it! Now how can we detect IE6 with another dozen bytes or less?
No. 8 — January 29th, 2009 at 8:00 am
Easy to beat:
I=screenTop
11 π
No. 9 — January 29th, 2009 at 8:08 am
Or, if you allow a null value as IE proof:
I=onhelp
8. π
No. 10 — January 29th, 2009 at 8:25 am
@rvdh
Yeah but the problem is that onhelp can be defined by a web site. So if a js library wanted to detect which browser it could be wrong.
@MJ
Because this was fun I’m gonna start a new post with some browser detecting hacks π
No. 11 — January 29th, 2009 at 9:29 am
onhelp is part of the window property: this.onhelp, therefore you can check if it’s null (which it should be) if it’s set and true, you still know it’s IE cuz the others will fail this property, And of course, “IE” can be overridden as well. π
No. 12 — January 29th, 2009 at 9:41 am
@rvdh
Yes initially I had the same thoughts but when you think of it in the context of a js library your detection has to be independent of the web site code. E.g. this returns true in Firefox:-
onhelp=function() {
alert(‘Some web site code’);
}
if(onhelp) {
alert(‘Huh this is FF’);
}
No. 13 — January 8th, 2010 at 3:28 am
better decision from me:
!-[1,]
returns true in IE and false in all others
or even
-[1,]
returns NaN in IE and -1 in all other browsers