↑
Main Page
Similarities
Property/Method Type R/W Description
stop
Function N/A You can call this method to prevent the further
Propagation()
propagation (bubbling) of the event.
target
Element R
The element/object that caused the event
timeStamp
Long R
The time that this event occurred in milliseconds after
midnight on January 1, 1970
type
String R
The name of the event
Similarities
Here is a brief roundup of the similarities between the two event objects.
Getting the event type
In order to get the event type in either browser, use the following:
var sType = oEvent.type;
This returns a value such as
“click”
or
“mouseover”
and is useful when one function is used as an
event handler for two different events. For example:
function handleEvent(oEvent) {
if (oEvent.type == “click”) {
alert(“Clicked!”);
} else if (oEvent.type == “mouseover”) {
alert(“Mouse Over!”);
}
}
oDiv.onclick = handleEvent;
oDiv.onmouseover = handleEvent;
In this code, the function
handleEvent()
is assigned as an event handler for both the
click
and
mouseover
events. Inside of the function, the
type
property is used to determine which course of action
should be taken.
Note that this example uses the DOM method of passing in the event object, but the code inside the
function can also be used in Internet Explorer after the
event
object has been assigned to
oEvent
.
Getting the key code (keydown/keyup events)
During a
keydown
or
keyup
event, you can retrieve the code for the key that was pressed by using the
keyCode
property:
var iKeyCode = oEvent.keyCode;
The
keyCode
property always contains a code the represents the key pressed, which may or may not
represent a character. For example, the Enter (or Return) key has a
keyCode
of 13, the space bar has a
keyCode
of 32, and the BackSpace key has a
keyCode
of 8.
274
Chapter 9
12_579088 ch09.qxd 3/28/05 11:39 AM Page 274
Free JavaScript Editor
Ajax Editor
©
→