This chapter describes the DOM Level 2 Event Model as implemented by Gecko. The Event interface itself is described, as well as the interfaces for event registration on nodes in the DOM, event handlers and event listeners, and several longer examples that show how the various event interfaces relate to one another.
There is an excellent diagram that clearly explains the three phases of event flow through the DOM in the DOM Level 3 Events draft.
Event interface Event handlers may be attached to various elements in the DOM. When an event occurs, an event object is dynamically created, and passed sequentially to the event listeners that are allowed to handle the event. The DOM Event interface is then accessible within the handler function, via the event object passed as the first (and the only) argument.
The following simple example shows how an event object is passed to the event handler function, and can be used from within one such function.
Note there is no "evt" parameter passed in the code below. The event object gets passed automatically to foo. All that is needed is to define a parameter in the event handler to receive the event object.
function foo(evt) {
// event handling functions like this one
// get an implicit reference to the event object they handle
// (in this case we chose to call it "evt").
alert(evt);
}
table_el.onclick = foo;
This example is woefully simplistic, but it shows an important feature of events in the Gecko DOM, which is that event objects in the DOM are typically accessed in the event handler functions. Once you have a reference to the event object, you can access all of the properties and methods described in this chapter.
Also see Example 5: Event Propagation in the Examples chapter for a more detailed example of how events move through the DOM.
In addition to the event object described here, the Gecko DOM also provides methods for registering event listeners on nodes in the DOM, removing those event listeners, and dispatching events from the DOM. These and the various Event Handlers on HTML or XML elements are the main entry points for events in the DOM. These three methods are described in the DOM Element Reference list.
You can also pass the event object reference as a predefined parameter, named event, to the function that handles the event. This is very similar to the way this works, but for event objects, rather than element object references.
<html>
<head>
<title>event object parameter example</title>
<script type="text/javascript">
function showCoords(evt){
alert(
"clientX value: " + evt.clientX + "\n" +
"clientY value: " + evt.clientY + "\n"
);
}
</script>
</head>
<body onmousedown="showCoords(event)">
<p>To display the mouse coordinates click anywhere on the page.</p>
</body>
</html>
Using the predefined event object parameter allows you to pass other parameters to the event handling function as well, if required:
<html>
<head>
<title>event object & extra parameters example</title>
<script type="text/javascript">
var par2 = 'hello';
var par3 = 'world!';
function showCoords(evt, p2, p3){
alert(
"clientX value: " + evt.clientX + "\n"
+ "clientY value: " + evt.clientY + "\n"
+ "p2: " + p2 + "\n"
+ "p3: " + p3 + "\n"
);
}
</script>
</head>
<body onmousedown="showCoords(event, par2, par3)">
<p>To display the mouse coordinates please click anywhere on the page.</p>
</body>
</html>
<alt> key was pressed during the event.
<ctrl> key was pressed during the event.
meta key was pressed during the event.
<shift> key was pressed when the event was fired.
AbstractView from which the event was generated.
DocumentEvent interface.
Page last modified 03:58, 14 Aug 2008 by Brettz9