↑
Main Page
EventUtil
To make this easy, just go straight down the table of properties and methods and try to make IE comply
with the DOM model. The
altKey
property is already there, the
bubbles
property cannot be recreated,
the
button
property is there, the
cancelBubble
property is there, and the
cancelable
property cannot
recreated — that brings up the
charCode
property.
As mentioned earlier, in IE the character code is contained in the
keyCode
property on the
keypress
event; otherwise it’s the correct value. So, if the type of event is
keypress
, it’s logical to create a
charCode
property that is equal to
keyCode
; otherwise, the
charCode
property should be set to
0
:
EventUtil.formatEvent = function (oEvent) {
if (isIE && isWin) {
oEvent.charCode = (oEvent.type == “keypress”) ? oEvent.keyCode : 0;
}
return oEvent;
};
Continuing down the table, the
clientX
,
clientY
, and
ctrlKey
properties are all the same in IE as in
the DOM. We can’t accurately recreate
currentTarget
or
detail
, so leave those off. However, you can
put a value for
eventPhase
. This property is always equal to
2
for the bubbling phase because that is all
IE supports:
EventUtil.formatEvent = function (oEvent) {
if (isIE && isWin) {
oEvent.charCode = (oEvent.type == “keypress”) ? oEvent.keyCode : 0;
oEvent.eventPhase = 2;
}
return oEvent;
};
Next in the table is the
isChar
property, which is
true
if the
charCode
property is not
0
:
EventUtil.formatEvent = function (oEvent) {
if (isIE && isWin) {
oEvent.charCode = (oEvent.type == “keypress”) ? oEvent.keyCode : 0;
oEvent.eventPhase = 2;
oEvent.isChar = (oEvent.charCode > 0);
}
return oEvent;
};
The
keyCode
property is the same in both browsers, and the
metaKey
property cannot be recreated in
IE, so that brings up
pageX
and
pageY
. Although the IE
event
object doesn’t have equivalent properties,
these properties can be calculated by taking the
clientX
and
clientY
values and augmenting them
with the
scrollLeft
and
scrollTop
values of the document body:
EventUtil.formatEvent = function (oEvent) {
if (isIE && isWin) {
oEvent.charCode = (oEvent.type == “keypress”) ? oEvent.keyCode : 0;
oEvent.eventPhase = 2;
oEvent.isChar = (oEvent.charCode > 0);
oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
oEvent.pageY = oEvent.clientY + document.body.scrollTop;
296
Chapter 9
12_579088 ch09.qxd 3/28/05 11:39 AM Page 296
Free JavaScript Editor
Ajax Editor
©
→