32 bytes, ehr ... 9, ehr ... 7!!! to know if your browser is IE
Update another clever trick from comments, which I will explain later:
it works, doesn't it?
The reason is simple, if we consider "\v" as a vertical space, browsers interpreter that check in this way:
where "not one" is always false, considering that 0 is always casted as false, and every other number is casted as true.
Internet Explorer will ignore the vertical space, trying to cast a "v" character into a number, thanks to the sign (plus or minus, it does not matter)
I read a lot of comments in both Ajaxian and other places ... well, guys, this is a trick as conditional comment is.
The day IE will interpret correctly the "\v" character, Opera or some other browser could implement conditional comments as well.
That's my simple point of view:
conditional comments could be stripped out from minifiers (not MyMin) and make code difficult do understand or maintain while this 7 bytes trick is portable, minifier/packer safe, and extremely easy to maintain (the day IE will be able to return a false, we'll have solved almost every cross browser problem thanks to JS2 but just in case, all we need to do, will be a simple replace with the isIE.Next trick)
Note this trick is library/hack proof. If you think that execScript check is enough, for example, consider this: top["execScript"]=null and/or libraries that implements execScript for other browsers.
if(!+"\v1") // true only in IEif("\v"=="v")// true only in IEtry{IE=window=!1}catch(e){IE=!0}
it works, doesn't it?
The reason is simple, if we consider "\v" as a vertical space, browsers interpreter that check in this way:
!(+
1
)
where "not one" is always false, considering that 0 is always casted as false, and every other number is casted as true.
Internet Explorer will ignore the vertical space, trying to cast a "v" character into a number, thanks to the sign (plus or minus, it does not matter)
// How IE interpreters the code
!(+"v")
// where
isNaN(+"v") === true
// and where NaN is implicitly casted as false
About conditional comments
I read a lot of comments in both Ajaxian and other places ... well, guys, this is a trick as conditional comment is.
The day IE will interpret correctly the "\v" character, Opera or some other browser could implement conditional comments as well.
That's my simple point of view:
conditional comments could be stripped out from minifiers (not MyMin) and make code difficult do understand or maintain while this 7 bytes trick is portable, minifier/packer safe, and extremely easy to maintain (the day IE will be able to return a false, we'll have solved almost every cross browser problem thanks to JS2 but just in case, all we need to do, will be a simple replace with the isIE.Next trick)
Note this trick is library/hack proof. If you think that execScript check is enough, for example, consider this: top["execScript"]=null and/or libraries that implements execScript for other browsers.
Comments
Post a Comment