JavaScript
  Home arrow JavaScript arrow Page 3 - Handling events with the DOM, part II
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 II
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2005-04-26

    Table of Contents:
  • Handling events with the DOM, part II
  • Assigning event handlers in NS6 and Mozilla
  • Assigning event handlers in IE5
  • Using the "attachEvent()" method: a simple drop-down menu in IE

  • 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 II - Assigning event handlers in IE5


    (Page 3 of 4 )

     

    As in many other cases, IE5+ has implemented a proprietary version of the DOM for managing events. In fact, it’s much more simplistic when dealing with event handlers, since it focuses only during the event bubble phase. There is a major drawback with this approach, because it’s very far from the W3C DOM proposed standards. According to IE, there are two main proprietary methods available for assigning event handlers:

     

    attachEvent(eventname,function)

     

    and

     

    detachEvent(eventname,function)

     

    In a similar fashion, methods are annexed to objects in the following manner:

     

    object.attachEvent(eventname,function)
    object.detachEvent(eventname,function)

     

    Let’s specify the corresponding parameters for the above methods:

     

    Eventname: references the full event name that must be detected, such as "onclick", "onload", and so forth.

     

    Function: contains the reference call to the function to be executed when the event is bubbled. Once again, a reminder is needed here: at the time that we’re passing the function name, its parenthesis must be removed.

     

    The first "attachEvent()" method assigns an event handler to an object in IE5+, while the second removes it from the object. When we’re removing an event handler with the "detachEvent()" method, all of the parameters should match those previously specified in its counterpart "attachEvent()". Let’s illustrate some examples, to properly understand how they are implemented:

     

    <script language="javascript">

        alertBox=function(){

              alert('Reacting to Event bubble phase');

        }

        div=document.getElementById('testdiv');

        div.attachEvent('onclick',alertBox);

    </script>

     

    And the HTML markup should be written like this:

     

    <div id="testdiv">

    <p><a href="#">Click here activate the alert method</a></p>

    </div>

     

    If we need to dynamically remove the event handler, all we need to do is call the "detachEvent()" method in the following manner:

     

    div.detachEvent('onclick',alertBox);

     

    Please note that we’ve used the same parameters originally passed to "attachEvent()" to remove the event itself from the object.

     

    Analogously, we can call the "attachEvent()" method multiple times, assigning many event handlers to the same object and executing different functions. For instance:

     

    <script language="javascript">

        alertBox=function(){

              alert('Reacting to Event bubble phase');

        }

        largeAlertBox=function(){

              alert('Reacting to Event bubble phase\nReacting to Event bubble phase\nReacting to Event bubble phase');

        }

        var div=document.getElementById('testdiv');

        div.attachEvent('onmouseover',alertBox);

        div.attachEvent('onmouseout',largeAlertBox);

    </script>

     

    In the previous example, both functions will be executed in sequence, first when the mouse is over the <div> element, and second when the mouse is positioned outside. Finally, as we explained for the "addEventListener()" method, it’s feasible to call different functions responding to the same event, without removing any functions from execution. Utilizing the previous example, the code would look like this:

     

    <script language="javascript">

        alertBox=function(){

              alert('Reacting to Event bubble phase');

        }

        largeAlertBox=function(){

              alert('Reacting to Event bubble phase\nReacting to Event bubble phase\nReacting to Event bubble phase');

        }

        window.attachEvent('onload',alertBox);

        window.attachEvent('onload',largeAlertBox);

    </script>

     

    Since IE sticks to the event bubble model, when the page is loaded, the two functions will be executed in the following order: first,  "largeAlertBox()" and then, "alertBox()", without overriding each other. As we’ve indicated previously, this approach is extremely handy when we’re dealing with multiple scripts that need to be registered to the same event handler.

     

    As was mentioned some time ago, browser incompatibilities still run deep in the area of event handling. Still, there are some workarounds to deal with these inconsistencies. Most of the time, this involves writing a function that takes care of handling internally the methods above described, according to the DOM version implemented. In this way, we can manage to create something close to a cross-browser solution.

     

    In next section, we get practical, presenting a simple drop-down menu example as part of the learning process for handling event handlers in IE5+.

     

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this second part, the article is focused mainly on the propietary methods offered...
     

    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 5 hosted by Hostway