I just ran across a post by David Flanagan, wherein he mentions that Internet Explorer on a Windows machine, XP in this case, doesn’t agree with its own understanding of global objects, namely the Window object.
The code he illustrates that does not behave as expected is:
window.onload = function()
{
alert(window.event == window.event);
}
For some strange reason, Internet Explorer believes this comparison is false. To fix this behavior, or rather, *tell* Internet Explorer how it should behave I propose resolution. You can achieve resolution in one of two ways.
var we = window.event;
alert(we == we); // true in IE
*OR*
window.onload = function()
{
alert(!!window.event == !!window.event);
}
It should be mentioned, that the double NOT’s first create a Boolean conversion, returning the opposite (logical NOT), then reverses what was returned. I forgot exactly who coined this particular use, but it was first made apparent to me by a very talented gentleman by the name of “RobG” in the comp.lang.javascript newsgroup.
This process is however called normalization, well, not in a database-sense, but anyway… see “A double use of the ! operator…”
*OR*
window.onload = function()
{
alert(Boolean(window.event) == Boolean(window.event));
}
“Um, but you said 2 methods, toe.” Right, I know. But the last method is doing the same as the second method, only much cleaner and technically faster.
Granted, as Mr. Flanagan explains, “This probably does not have any impact on real-world code.” But nonetheless quirky, well, rather downright stupid oddities in one of the most mainstream browsers (sadly) is an oddity that all JavaScript developers should be aware of.