February 10, 2000 - Passing Events in Navigator
February 10, 2000 Passing Events in Navigator Tips: February 2000
Yehuda Shiran, Ph.D.
|
true
or false
, an event processing function can also pass an event to a specific event receiver. Any object that can register event handlers is an event receiver. By calling the event handler of a specific event receiver, you can bypass the event capturing hierarchy. For instance, if you want to capture all mouse clicks in the document, except for those generated by a specific button, you can check if the event's intended target is that button. If so, simply send it to the button object via the handleEvent()
method.
The handleEvent()
method explicitly calls the event handler of the desired event receiver, so it takes over and handles the event immediately. For example, if you want all click events to go to the first link on the page, you could use the following event processing function:
function processClicks(e) {
document.links[0].handleEvent(e);
}
Of course, the link must have an event handler to process the event. The following code segment shows how to put the preceding function to work:
<A HREF="...">...</A><!-- the first link in the document -->
<SCRIPT LANGUAGE="JavaScript">
<!--
function processClicks(e) {
document.links[0].handleEvent(e);
}
function firstLink(e) {
alert("The first link's event handler called this function.");
}
window.captureEvents(Event.CLICK);
window.onclick = processClicks;
document.links[0].onclick = firstLink;
// -->
</SCRIPT>
If you put this script in an independant HTML document (so its link is first), the
firstLink()
function is invoked whenever you click the mouse (in the browser area), regardless of the pointer's position. That is, you do not have to click the link directly, because the window object captures all click events and sends them to the first link's event handler, which calls the firstLink()
function.
Learn more about Netscape Navigator's event model in Column 9, The Navigator Event Model.