Left vs Middle vs Right Mouse Button
Question: How do I check whether the user clicked the left or right mouse button? Can I use an
Answer:
The event.which in Firefox and Netscape Navigator (1 is left, 2 is middle, 3 is right)
event.button in Microsoft Internet Explorer (1 is left, 4 is middle, 2 is right)
event.button in Firefox and other W3C browsers (0 is left, 1 is middle, 2 is right)
In this example the var sTestEventType='mousedown'; function handleMouseEvent(e) { var evt = (e==null ? event:e); var clickType = 'LEFT'; if (evt.type!=sTestEventType) return true; if (evt.which) { if (evt.which==3) clickType='RIGHT'; if (evt.which==2) clickType='MIDDLE'; } else if (evt.button) { if (evt.button==2) clickType='RIGHT'; if (evt.button==4) clickType='MIDDLE'; } alert(evt.type+': '+clickType+' button!'); return true; } document.onmousedown = handleMouseEvent; document.onmouseup = handleMouseEvent; document.onclick = handleMouseEvent; Note: Internet Explorer 8.x may fire |
[Test this event type:
mousedown|mouseup|click
For the purposes of this test, context menu is now
disabled.
Copyright © 1999-2011, JavaScripter.net.