HTML and CSS Reference
In-Depth Information
The event object is accessed within an event handler function, using the window object as
shown:
var evnt = window.event;
The event object is a property of the window object. In this example, a reference to the
event object is assigned to a variable but also can be used directly. Within the context of the
current event handler, the event object contains the pertinent information—that is to say, the
respective properties are set. For example, in a keydown event, the details of the keyboard
state are available, but the mouse buttons aren't because they aren't relevant to a keydown
event.
NOTE
ACCESSING THE EVENT CONTEXT
In Internet Explorer, the window event is the method required to access the event ob-
ject. However, in some browsers, the event object is passed to the event function as a
parameter.
Declarative event handling
Handling events declaratively in the HTML markup is possible by setting up an event handlers
line within the HTML elements. This is effectively no different than assigning a value to any
other property or attribute of the HTML element. Look at the following HTML sample:
<html>
<head>
<script>
function onloadHandler() {
alert("hello event.");
}
</script>
</head>
<body onload="onloadHandler();">
…
</body>
</html>
In this HTML markup, the onload attribute of the body element is assigned JavaScript to
run. The onload event fires when the document itself is fully loaded into the browser. When
the document is loaded, the onload event fires, which calls the onloadHandler function and in
turn shows the alert box. Any events that will be looked at through this objective can be set
up this way directly in the HTML markup. Next, you see how to set up events programmati-
cally by assigning the function to the event property in JavaScript.
 
Search WWH ::




Custom Search