JavaScript
  Home arrow JavaScript arrow Page 4 - 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  
Mobile Linux 
App Generation ROI 
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 - Using the "attachEvent()" method: a simple drop-down menu in IE


    (Page 4 of 4 )

     

    In an attempt to show how the proprietary "attachEvent()" method works for IE, let’s set up a simple drop down menu. To begin with, here’s the JavaScript function to be executed:

     

    <script language="javascript">

    buildMenu=function(){

        var menu=document.getElementById('menu');

        if(!menu){return;}

        var menuitem=document.getElementById('menuitem');

        if(!menuitem){return;}

        hideMenu=function(){

              menuitem.style.display='none';

        }

        showMenu=function(){

              menuitem.style.display='block';

        }

        menu.attachEvent('onmouseover',showMenu);

        menu.attachEvent('onmouseout',hideMenu);

    }

    window.attachEvent('onload',buildMenu);

    </script>

     

    And here are the CSS declarations that we’ve tied to the HTML code:

     

    <style type="text/css">

    #menu {

        position: absolute;

        top: 20px;

        left: 20px;

        padding: 0px;

        width: 150px;

        background: #36c;

        border: 1px solid #000;

        font: bold 11px "Verdana", Arial, Helvetica, sans-serif;

        color: #fff;

        padding: 1px;

    }

    .menuitem {

        display: none;

        background: #9cf;

        padding: 4px;

        border-top: 1px solid #000;

    }

    a:link,a:visited {

        display: block;

    }

    a:hover {

        color: #f00;

    }

    </style>

     

    Finally, here’s the complete listing for the HTML markup:

     

    <div id="menu">Resources

    <div class="menuitem" id="menuitem">

    <a href="http://www.devshed.com" title="Open source articles">Open Source</a>

    <a href="http://www.devarticles.com" title="Web development articles">Web Development</a>

    <a href="http://www.google.com" title="Find what you are looking for!">Google it!</a>

    </div>

    </div>

     

    Looking at the example listed above, we can clearly see that the drop-down menu is really simplistic. Let’s dissect the code, to understand what’s happening. In our CSS declarations, we defined a "menu" contextual selector that references the menu general container. The element is absolutely positioned and styled by applying font typefaces, colors, and so forth. Then, we declared the "menuitem" class for styling menu items. Finally, we set up to display the menu links as block-level elements. Nothing unexpected, right?

     

    As part of the HTML, two <div> elements have been defined, the first one being the menu container, and the second the containing element for menu items. Now, it’s time to take a look at the JavaScript "buildMenu()" function. Its logic relies on grabbing the proper menu and menu items elements for manipulating them in the following sequence: when the mouse is over the menu container, it fires up the function that displays menu items, setting its "display" property to "block". During the opposite stage, when the mouse is outside the menu container, menu items are hidden from view, resetting the display’s property value to "none".

     

    For both stages, we used the "attachEvent()" method to tie the "onmouseover" and "onmouseout" event handlers to the menu element, and execute each required specific function. Here, we have a situation where two different functions have been attached to the same object. Finally, we tie the "buildMenu()" function to a window object for being executed when the page is loaded.

     

    Certainly the sample is easy to grasp. However, not surprisingly, the major drawback with this approach is that it only works for IE. The code might be easily changed into a nearly cross-browser version by replacing the calls to the "attachEvent()" method, with the standard "onmouseover" and "onmouseout" expressions, and substituting the final call to the "onload" event handler with a "window.onload=buildMenu" statement. I will leave that to you as a little homework. After all, working with event handlers is such a fun experience, isn’t it?

     

    Summary

     

    That’s all for now. We’ve deeply explained the main methods available in the major browsers for manipulating event handlers, as well as shown some simple cases, in order to illustrate a practical usage for them. In the last part of this series, we’ll be focusing our attention on the DOM "event" object, looking at its numerous properties and methods, and hopefully providing a handy reference for fast and easy implementation in Web projects. Until then, stay tuned.  


    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.

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

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - 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






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