6.2.2. Event Object Properties
Except for a handful of
frequently-used and important properties, the IE and W3C
event objects share a number of property names.
Table 6-3 lists the most common DHTML-related
event object properties in both event models, plus
several position-related properties from Netscape 6 and later that
are not part of the W3C event model.
Table 6-3. Equivalent properties of the IE and Netscape 6 event objects
IE property
|
Description
|
W3C property or method
|
altKey
|
The Alt key was pressed during the
event (Boolean)
|
altKey
|
button
|
The mouse button pressed in the mouse event (Integer, but different
numbering systems per model)
|
button
|
cancelBubble
|
Whether the event should bubble further
|
stopPropagation( )
|
clientX, clientY
|
The horizontal and vertical coordinates of the event in the content
region of browser window
|
clientX, clientY
|
ctrlKey
|
The Ctrl key was pressed during the
event (Boolean)
|
ctrlKey
|
fromElement
|
The object or element from which the pointer moved for a
mouseover or mouseout event
|
relatedTarget
|
keyCode
|
The keyboard character code of a keyboard event (Integer)
|
keyCode
|
offsetX, offsetY
|
The horizontal and vertical coordinates of the event within the
element space
|
Calculated from other properties
|
Calculated from other properties
|
The horizontal and vertical coordinates of the event within the
document space (Netscape only)
|
pageX, pageY
|
returnValue
|
The value returned to the system by the event (used to prevent
default action in IE)
|
preventDefault( )
|
screenX, screenY
|
The horizontal and vertical coordinates of the event relative to the
screen
|
screenX, screenY
|
shiftKey
|
The Shift key was pressed during
event (Boolean)
|
shiftKey
|
srcElement
|
The object or element intended to receive the event
|
target
|
toElement
|
The object or element to which the pointer moved for a
mouseover or mouseout event
|
relatedTarget
|
type
|
The name of the event (without "on"
prefix)
|
type
|
x, y
|
The horizontal and vertical coordinates of the event within
body element (for unpositioned target) or
relative-positioned element
|
layerX, layerY
|
Of all the properties listed in Table 6-3, the pair that you will most likely call upon
are the ones that refer to the element from which the event object
was created. Microsoft calls the element the
srcElement, while the W3C calls it the
target. For a given event handler executing in
either browser, the respective properties return a valid reference to
the same element. Using object detection techniques, a typical
skeleton structure for an event handler function is as follows:
function myFunction(evt) {
evt = (evt) ? evt : ((event) ? event : null);
if (evt) {
var elem = (evt.target) ? evt.target :
((evt.srcElement) ? evt.srcElement : null);
if (elem) {
// act on element receiving event
...
}
}
}
Once your script has a reference to the element receiving the event,
it's easy to use identical, cross-DOM syntax for
many DHTML operations, such as modifying style property values.
Obviously, this kind of branching is needed only when you must refer
to incompatible property names. For event data on mouse button or
keyboard actions, you can work directly from the equalized reference
to the event object.