I have a JavaScript function that handles mouse button events. It should be able to distinguish between left and right mouse buttons. Unfortunately, Internet Explorer uses different values ββfor event.button than all other browsers. I know how to interpret them, but I need to know which way to go.
I did this using a JavaScript hack that relies on conditional compilation. This is something like this:
if (false) { IE fixup... }
I consider this rather safe method because it is based on JavaScript parser features that cannot be faked and are unlikely to be simulated by other browsers.
Now I use the Google Closure compiler to pack JavaScript files. I found that it removes conditional compilation comments just like any other comments. So I tried different hacks. One of them:
if ("\v" == "v") { IE fixup... }
Unfortunately, the closure compiler is pretty smart and discovers that the condition can never be true and removes this code. Also, I donβt like it because Microsoft may end up fixing this \ v error and then the detection will fail.
I could just read something like navigator.appName or what it called, but it's too easy to fake. And if someone changes the identity of their browser, they are unlikely to be able to implement other event.button behavior ...
The Closure compiler allows you to save specific comments. I tried this:
if (false) { IE fixup... }
, . JavaScript , .
- , JS ?
, :
function findEvent(e)
{
e = e || event;
if (!e.target && e.srcElement)
e.target = e.srcElement;
if (isSet(e.button))
{
if (false)
{
if (e.button & 1)
e.mouseButton = 0;
else if (e.button & 2)
e.mouseButton = 2;
else if (e.button & 4)
e.mouseButton = 1;
}
else
e.mouseButton = e.button;
}
return e;
}