Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

The JavaScript FAQ
Prev Home Next

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

    The JavaScript FAQ
    Prev Home Next


  •  
     
      Mirrored with kind permission of Alexei Kourbatov Design by Interspire