SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "developer.mozilla.org" button script doesn't work, why?

    Hi
    I have been trying to learn is much as I can about events in JavaScript. One of the sites I visited in this pursuit was "developer.mozilla.org". At that site I found the script below which is supposed to generate and an alert box telling you whether you click over the paragraph, included in the HTML, with the right mouse button or the left mouse button or the center mouse button. It only works on the left mouse button on my computer in both Internet Explorer and Firefox so please tell me why. I would appreciate it.

    Sincerely
    Marc

    Link to the script I'm talking about

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Mouse which Button</title>
    <script type="text/javascript">
    
    function whichButton(e)
    {
      // Handle different event models
      var e = e || window.event;
      var btnCode;
    
      if ('object' == typeof e){
        btnCode = e.button;
    
        switch (btnCode){
          case 0  : alert('Left button clicked');
                    break;
          case 1  : alert('Middle button clicked');
                    break;
          case 2  : alert('Right button clicked');
                    break;
          default : alert('Unexpected code: ' + btnCode);
        }
      }
    }
    
    </script>		
    </head>
    
    <body>
    <p onclick="whichButton(event);">Click with mouse...</p>
    </body>
    </html>

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is strange as onmousdown gives you all three buttons, but theres a difference in the button code between browsers.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •