JavaScript
  Home arrow JavaScript arrow Page 3 - Handling events with the DOM – Part I
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  
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 I
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-04-19

    Table of Contents:
  • Handling events with the DOM – Part I
  • Back to basics: assigning event handlers
  • Understanding the DOM event flow: Event Capture and Event Bubble
  • Inline and scripted event handlers into action

  • 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 I - Understanding the DOM event flow: Event Capture and Event Bubble


    (Page 3 of 4 )

     

    Detecting events and assigning the proper event handlers with the DOM is really a straightforward process, introducing a new manner for doing this. What’s more, the two conventional approaches previously described are perfectly supported and valid. For a complete understanding of assigning event handlers with the DOM, it’s really necessary first to explain the way that events are handled within its context. 

    Let’s assume we’re representing an extremely common situation, where the user passes the mouse over a regular link present in a Web document. From a user’s point of view, the process consists of just hovering on the link and that’s all. Period. On the other hand, for the DOM, things are more complex and technical, generating a set of processes that involves two phases, called Event Capturing and Event Bubbling respectively. According to the previous example, when a user is passing the mouse over the link, these are the events that take place, in the following order: 

    • The user moves the mouse over the document.

    • The user moves the mouse over any tag containing the target <a> element.

    • The user moves the mouse over the specific target <a> element. 

    The two processes prior to reaching the target <a> element are defined as taking place at the event capturing phase. Once the event has reached the target, it travels back in the following way: 

    • The user moves the mouse over any tag containing the target <a> element.

    • The user moves the mouse over the document. 

    The two last steps involve the event bubbling phase. 

    As we can see, the complete process, including the two phases, is quite lengthy, and considerably different from a user’s point of view. In order to clarify this explanation, here are a couple of diagrams that show the entire event, as interpreted by theDOM: 

     

    The above images illustrate the event capturing and the event bubble phases, according to the model implemented by the W3CDOM. As we’ll see shortly, there are significant differences between the way that today’s browsers support event bubbling and event capturing. 

    As we can deduce from the above example diagrams, the concept of Event Capture basically references the phase when the event is intercepted, or more familiarly when the event is traveling through the document, downwards, traversing all of the containing elements of the target, until it reaches the target itself. In the opposite way, Event Bubble references the phase when the event is traveling from the target element upwards, traversing the containing elements of the target, to the topmost element, that is, the document element. 

    Certainly, NS4 has been supporting event capturing for a long time with the method: 

    element.captureEvents() 

    On the other hand, IE4 was the first major browser that offered event bubble capabilities. It's worth noting that Microsoft was an early implementer of many W3C working drafts and recommendations, but in the event handling area, it certainly has adopted several approaches that differ noticeably from the proposed W3C DOM standards. 

    Now, let’s step back to some practical concepts and give a better idea of event bubble and capturing definitions. Let’s present an example using some basic HTML markup and both approximations: inline and scripted event handlers. 

    More JavaScript Articles
    More By Alejandro Gervasio


       · In the first part of the series, the concepts of bubble and capture phase are...
     

    JAVASCRIPT ARTICLES

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - 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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT