↑
Main Page
Getting the event object
oEvent.eventPhase = 2;
oEvent.isChar = (oEvent.charCode > 0);
oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
oEvent.pageY = oEvent.clientY + document.body.scrollTop;
oEvent.preventDefault = function () {
this.returnValue = false;
};
if (oEvent.type == “mouseout”) {
oEvent.relatedTarget = oEvent.toElement;
} else if (oEvent.type == “mouseover”) {
oEvent.relatedTarget = oEvent.fromElement;
}
oEvent.stopPropagation = function () {
this.cancelBubble = true;
};
oEvent.target = oEvent.srcElement;
oEvent.time = (new Date).getTime();
}
return oEvent;
};
Because the
type
property is the same in both IE and the DOM, this is the end of the method. However,
this method isn’t intended to be used alone. Instead, it is intended to be used inside of another method
that gets a reference to the
event
object.
Getting the event object
Unfortunately, IE and the DOM use very different methods to get the
event
object. In IE, the
event
object is tied to the
window
object although in the DOM it is independent of any other object and is
passed in as an argument. Because of this, it is very difficult to make IE’s event model act like Mozilla’s,
or vice versa. Instead of trying to make one like the other, you can create a new method that can be used
by both browsers called
getEvent()
.
The
getEvent()
method accepts no arguments and its sole purpose is to return the event object. The
first case it deals with is IE, checking for the existence of
window.event
and then using
formatEvent()
before returning the
event
object:
EventUtil.getEvent = function() {
if (window.event) {
return this.formatEvent(window.event);
}
};
Next up is the DOM case. Remember, DOM-compliant browsers pass the
event
object as an argument
to the event handler. This is when it pays to remember that a function is actually an object that has prop-
erties. In this case, the property of interest is called
caller
.
299
All about Events
12_579088 ch09.qxd 3/28/05 11:39 AM Page 299
Free JavaScript Editor
Ajax Editor
©
→