event
object and the event handlers that are used with client-side objects in JavaScript to evoke particular actions. In addition, it contains general information about using events and event handlers.
Table 9.1 lists the one object in this chapter. Table 9.1 Event-related object
Object |
Description
|
Represents a JavaScript event. Passed to every event handler.
| |
---|
Table 9.2 summarizes the JavaScript event handlers.
Table 9.2 Events and their corresponding event handlers.
General Information about Events
JavaScript applications in the browser are largely event-driven. Events are actions that occur usually as a result of something the user does. For example, clicking a button is an event, as is changing a text field or moving the mouse over a link. For your script to react to an event, you define event handlers, such as onChange
and onClick
.
Defining Event Handlers
If an event applies to an HTML tag, then you can define an event handler for it. The name of an event handler is the name of the event, preceded by "on"
. For example, the event handler for the focus event is onFocus
.
To create an event handler for an HTML tag, add an event handler attribute to the tag. Put JavaScript code in quotation marks as the attribute value. The general syntax is
<TAG eventHandler="JavaScript Code">
where TAG
is an HTML tag and eventHandler
is the name of the event handler. For example, suppose you have created a JavaScript function called compute
. You can cause the browser to perform this function when the user clicks a button by assigning the function call to the button's onClick
event handler:
<INPUT TYPE="button" VALUE="Calculate" onClick="compute(this.form)">
You can put any JavaScript statements inside the quotation marks following onClick
. These statements are executed when the user clicks the button. If you want to include more than one statement, separate statements with a semicolon.
When you create an event handler, the corresponding JavaScript object gets a property with the name of the event handler in lower case letters. (In Navigator 4.0, you can also use the mixed case name of the event handler for the property name.) This property allows you to access the object's event handler. For example, in the preceding example, JavaScript creates a Button
object with an onclick
property whose value is "compute(this.form)"
.
Chapter 7, "JavaScript Security," in JavaScript Guide contains more information about creating and using event handlers.
Events in Navigator 4.0
In Navigator 4.0, JavaScript includes event
objects as well as event handlers. Each event has an event
object associated with it. The event
object provides information about the event, such as the type of event and the location of the cursor at the time of the event. When an event occurs, and if an event handler has been written to handle the event, the event
object is sent as an argument to the event handler.
Typically, the object on which the event occurs handles the event. For example, when the user clicks a button, it is often the button's event handler that handles the event. Sometimes you may want the Window
or document
object to handle certain types of events. For example, you may want the document
object to handle all MouseDown events no matter where they occur in the document. JavaScript's event capturing model allows you to define methods that capture and handle events before they reach their intended target.
In addition to providing the event
object, Navigator 4.0 allows a Window
or document
to capture and handle an event before it reaches its intended target. To accomplish this, the Window
, document
, and Layer
objects have these new methods:
captureEvents
releaseEvents
routeEvent
handleEvent
(Not a method of the Layer
object)window.captureEvents(Event.CLICK);The argument to
Window.captureEvents
is a property of the event
object and indicates the type of event to capture. To capture multiple events, the argument is a list separated by vertical slashes (|). For example:
window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP)
Next, you need to define a function that handles the event. The argument evnt
is the event
object for the event.
function clickHandler(evnt) {
You have four options for handling the event:
//What goes here depends on how you want to handle the event.
//This is described below.
}
function clickHandler(evnt) { return true; }
function clickHandler(evnt) { return false; }
routeEvent
. JavaScript looks for other event handlers for the event. If another object is attempting to capture the event (such as the document), JavaScript calls its event handler. If no other object is attempting to capture the event, JavaScript looks for an event handler for the event's original target (such as a button). The routeEvent
method returns the value returned by the event handler. The capturing object can look at this return value and decide how to proceed. function clickHandler(evnt) {
var retval = routeEvent(evnt);
if (retval == false) return false;
else return true;
}
routeEvent
calls an event handler, the event handler is activated. If routeEvent
calls an event handler whose function is to display a new page, the action takes place without returning to the capturing object.
handleEvent
method of an event receiver. Any object that can register event handlers is an event receiver. This method explicitly calls the event handler of the event receiver and bypasses the capturing hierarchy. For example, if you wanted all click events to go to the first link on the page, you could use: function clickHandler(evnt) {
window.document.links[0].handleEvent(evnt);
}
onClick
handler, the link handles any click event it receives.window.onClick = clickHandler;
captureEvents
in a signed script and call
Window.enableExternalCapture
.
In the following example, the window and document capture and release events:
<HTML>
<SCRIPT>
function fun1(evnt) {
alert ("The window got an event of type: " + evnt.type +
" and will call routeEvent.");
window.routeEvent(evnt);
alert ("The window returned from routeEvent.");
return true;
}
function fun2(evnt) {
alert ("The document got an event of type: " + evnt.type);
return false;
}
function setWindowCapture() {
window.captureEvents(Event.CLICK);
}
function releaseWindowCapture() {
window.releaseEvents(Event.CLICK);
}
function setDocCapture() {
document.captureEvents(Event.CLICK);
}
function releaseDocCapture() {
document.releaseEvents(Event.CLICK);
}
window.onclick=fun1;
document.onclick=fun2;
</SCRIPT>
...
</HTML>
Last Updated: 10/31/97 16:34:02