JavaScript
  Home arrow JavaScript arrow Page 7 - Handling events with the DOM - Part III
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Handling events with the DOM - Part III
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-05-03

    Table of Contents:
  • Handling events with the DOM - Part III
  • Another creature in an object-based world: the Event object
  • Determining the type of event: the "type" property
  • Targeting objects: determining the target of the event
  • Focusing on keys: determining which key has been pressed
  • The x-y game: determining mouse coordinates
  • Buttons from the bottom: detecting mouse buttons

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Handling events with the DOM - Part III - Buttons from the bottom: detecting mouse buttons


    (Page 7 of 7 )

     

    In order to detect mouse buttons, we use two main properties: "which" and "button". We’ve already seen this Netscape property in key detection processes. However, these properties do not always work correctly when being used in a "click" event.

     

    The "which" property assigns the following values for mouse buttons:

     

    • Left button: 1
    • Middle button (mouse wheel): 2
    • Right button: 3

     

    The assignation sequence is logical, but currently, browser support is very limited. We need to look at the "button" property, which in the W3C event model uses the following values:

     

    • Left button: 0
    • Middle button: 1
    • Right button: 2

     

    In Netscape 6+, the values are different:

     

    • Left button: 1
    • Middle button: 2
    • Right button: 3

     

    And Mozilla assigns button values as follows:

     

    • Left button: 1
    • Middle button: 1
    • Right button: 2

     

    Once again, incompatibilities crop up, because IE interprets mouse buttons differently:

     

    • Left button: 1
    • Middle button: 4
    • Right button: 2

     

    Also, IE supports button combinations; for example, a value of 3, meaning that left and right buttons are combined, or 6, when the middle and right buttons are clicked at the same time. However, this is only available in the specifications. In practice, it’s not actually supported.

     

    Whew, here we have a wide gamut of button implementations, where none of them stick to a common sense assignment. Anyway, we can try to build a function for mouse button detection:

     

    function detectMouseButton(e){

        var mousebutton;

        if (!e) var e=window.event;

        if(e.button){mousebutton=e.button;}

        else if(e.which){mousebutton=e.which;}

        alert('Mouse button: '+mousebutton);

    }

     

    Certainly, this function is not very suitable for practical use, but as an example it is good enough. From the above specifications, it can be deduced that we’re able to detect right clicking events too. This is a little more useful than the previous case, for instance if we need to build a customized context menu. The basic function to detect a right click would be something similar to this:

     

    function detectRightClick(e){

        var rightclick='';

        if(!e) var e=window.event;

        if(e.which){

              (e.which==3)?rightclick='Right clicking!':rightclick='Not right clicking!';

        }

        else if(e.button){

              (e.button==2)?rightclick='Right clicking!':rightclick='Not right clicking!';

        }

        alert(rightclick);

    }

     

    And we call the function like this:

     

    document.onmousedown=detectRightClick;

     

    The above example will detect a right click event for most browsers, displaying the alert box with the corresponding messages according to the mouse buttons clicked. By the way, that’s not very creative, but it works pretty well. Another possibility for detecting right clicks is to implement the "oncontextmenu" event handler, but it functions only in IE and Mozilla. The JavaScript code is listed below:

     

    function detectRightClick(e){

        alert( 'Your are right clicking now!');

    }

    document.oncontextmenu=detectRightClick;

     

    It’s really simple, but not suitable for cross browser implementation. Anyway, if you’re working within an intranet environment, where the browser platform is IE, this might be a valid alternative to consider.

     

    Finally, we’ve developed valid solutions for solving common problems associated with event handling, and I feel that the whole experience was quite successful. The vast and intriguing arena of the Event Handling Model is not as difficult as it seems. Are you still with me? Fine, because we’re almost done. Just don’t miss the final part.

     

    Conclusion

     

    Over this series, we’ve covered the most relevant aspects of Event handling with the DOM. From the basics and core concepts regarding event handlers, to proper implementation for common event registration methods in Web documents, it’s been a long way to go. Despite the tremendous effort we've devoted to this so far, event handling is still a huge area to be mastered. However, it’s necessary to embrace the subject without feeling overcome by its complexity, building and reusing code that works for practical Web development applications. Having all of that knowledge at our disposal is more that enough to successfully face any event handling tasks.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This third part of the series shows several examples to tackle the most common...
       · Very helpful. You are an excelltent technical writer. Please submit more!
       · Hi, I'm glad to know that you've found this series helpful. I'm writing more...
       · It helped me finally get a popup menu working.Also made me aware of a completely...
       · Very glad to see that the article has helped you out in the developing...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway