spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column74


Netscape 6, Part III: The Event Model

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Setting Events in Netscape Navigator 4.x

In order to understand the new event handling strategy in Netscape 6, you need to refresh your knowledge of how it is done in Netscape Navigator 4.x. The main idea is that an event is diving, from the window object, through the document object and any other intermediate objects, until it reaches the target object. Netscape introduced the concept of event capturing. You can intercept an event at any object, before it reaches the final target element. Any such interception always occurs before the event reaches the target element. There is no way in Netscape Navigator 4.x to intercept an event after it reaches the target element. Let's demonstrate this strategy.

The piece of code below creates a button and two event handlers. We attach the first event handler to the window object. In order to do so, you need to instruct the window object to capture CLICK events:

window.captureEvents(Event.CLICK);

And then attach the action to this event:

window.onclick = windowClicked; // onclick is an event handler

We attach the second event handler to the button itself:

onClick="buttonClicked()"

Here is the full listing of the code:

<SCRIPT LANGUAGE="JavaScript">
<!--

function buttonClicked(e) {
  alert("You clicked a button!");
  return true;
}

function windowClicked(e) {
  alert("You clicked in the window!");
  return true;
}
if (window.captureEvents)
  window.captureEvents(Event.CLICK); // CLICK is an event
window.onclick = windowClicked; // onclick is an event handler

// -->
</SCRIPT>
<FORM>
<INPUT TYPE="button" VALUE="click here" NAME="myButton"
  onClick="buttonClicked()">
</FORM>

Get on Netscape Navigator 4.x and click the button below. You can see by the alert box that pops up ("You clicked in the window!") that the click event has been captured successfully by the window object, before it reaches the button. You won't get the button's alert box ("You clicked a button!"), because, by default, an event is always captured and not routed deeper to the next object in the hierarchy:

Next: How to set event handlers in Internet Explorer

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: January 1, 2001
Revised: January 1, 2001

URL: http://www.webreference.com/js/column74/2.html