JavaScript
  Home arrow JavaScript arrow Page 4 - 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 - Targeting objects: determining the target of the event


    (Page 4 of 7 )

     

    In this field, numerous differences are denoted, according to which browser we’re using. W3C and Netscape determine the target of the event through the "target" property. Conversely, IE uses the "srcElement" property. Our cross-browser function for detecting the target of the event can be coded as follows:

     

    function detectTarget(e){

        var targ;

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

        if (e.target){targ=e.target;}

        else if(e.srcElement){targ=e.srcElement;}

        if (targ.nodeType==3){targ=targ.parentNode;}

        alert(targ);

    }

     

    As you can see, we followed the same approach for object detection, assigning the property "target" for W3C/Nestcape and "srcElement" for IE.

     

    The checking process for node Type is suited to Safari. If an event occurs on an element that contains text, the text node, instead of the element, is considered the target of the event. For this reason we check whether the target's nodeTypeis 3, a text node. If it’s a textnode, we move up in the document tree to its parent node, that is, the HTML element.

     

    There are additional properties inherent to the target. One particularly useful one is "currentTarget." Suppose that we’ve defined one function that is triggered by two elements, where element 1 is a container of element 2, reacting to the same event. It is similar to this situation:

     

    element1.onclick=functionA;

    element2.onclick=functionA;

     

    Using a browser with event bubble capabilities, if the user clicks on element 2, "functionA" will be executed twice, since the event will fire the function for element 2 and traverse the document to reach element1, triggering again "functionA." In this case, how we know what HTML element is handling the event?

     

    The "target" and "srcElement" properties always reference the first element, since it’s the original source of the event. For addressing this issue, the W3C has implemented the "currentTarget" property. This contains a reference to the HTML element that is handling the event. Not surprisingly, IE doesn’t offer this property. However, based on the previous example, using the "this" keyword will reference the element handling the event, behaving similar to the "currentTarget" property.

     

    But if we’re using the IE registration model, with the attachEvent():

     

    element1.attachEvent('onclick',functionA);
    

    element2.attachEvent('onclick',functionA);

     

     

    The "this" keyword is not going to work to reference the HTML element handling the event. This is one of the most critical reasons why the IE registration model is not widely used.

     

    Given the situation explained above, sometimes it is desirable to manipulate the event flow related to page elements. For doing this, the W3C event model present two methods: "preventDefault()" and "stopPropagation()"respectively. The first can be used to cancel the event, if it can be cancelled. This prevents the browser from performing any default action for the event, such as loading a URL when a hypertext link is clicked. Note that the event will continue propagating along the normal event flow. The second simply stops the event flow. This can be used on either the capture or bubble phase.

     

    IE offers two properties (not methods) to achieve similar results. They are "returnValue" and "cancelBubble"The first property must be set to true to cancel any default action for the event, while the second should be set to true to prevent the event from bubbling.

     

    Now, let’s take a look at the properties for determining which key has been pressed.

     

    More JavaScript Articles
    More By Alejandro Gervasio


       · 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 2 hosted by Hostway