Left vs. Right Button
Question: How do I check whether the user clicked the left or right mouse button?
Answer:
The click
event occurs for the left mouse button only.
Therefore, onClick
event handlers do not need
to preform the left-versus-right button test.
On the other hand, the
mousedown
and mouseup
events may occur
for any mouse button. To determine whether the user
clicked the left or right button, you can use the following event properties:
event.which
in Netscape Navigator
event.button
in Internet Explorer
If the value of these properties is 1, the event occurred for the left button.
In the following example, the onMouseDown
event handler
displays the messages Left button or Right button,
depending on the mouse button you actually have clicked.
The messages will appear on your browser's status bar.
Click or right-click anywhere on this page to see it work:
<script language="JavaScript">
<!--
function mouseDown(e) {
if (parseInt(navigator.appVersion)>3) {
var clickType=1;
if (navigator.appName=="Netscape") clickType=e.which;
else clickType=event.button;
if (clickType==1) self.status='Left button!';
if (clickType!=1) self.status='Right button!';
}
return true;
}
if (parseInt(navigator.appVersion)>3) {
document.onmousedown = mouseDown;
if (navigator.appName=="Netscape")
document.captureEvents(Event.MOUSEDOWN);
}
//-->
</script>
JavaScripter.net.
Copyright
© 1999-2006, Alexei Kourbatov