18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9<br />

To make this easy, just go straight down the table of properties and methods and try to make IE comply<br />

with the DOM model. The altKey property is already there, the bubbles property cannot be recreated,<br />

the button property is there, the cancelBubble property is there, and the cancelable property cannot<br />

recreated — that brings up the charCode property.<br />

As mentioned earlier, in IE the character code is contained in the keyCode property on the keypress<br />

event; otherwise it’s the correct value. So, if the type of event is keypress, it’s logical to create a<br />

charCode property that is equal to keyCode; otherwise, the charCode property should be set to 0:<br />

EventUtil.formatEvent = function (oEvent) {<br />

if (isIE && isWin) {<br />

oEvent.charCode = (oEvent.type == “keypress”) ? oEvent.keyCode : 0;<br />

}<br />

return oEvent;<br />

};<br />

Continuing down the table, the clientX, clientY, and ctrlKey properties are all the same in IE as in<br />

the DOM. We can’t accurately recreate currentTarget or detail, so leave those off. However, you can<br />

put a value for eventPhase. This property is always equal to 2 for the bubbling phase because that is all<br />

IE supports:<br />

EventUtil.formatEvent = function (oEvent) {<br />

if (isIE && isWin) {<br />

oEvent.charCode = (oEvent.type == “keypress”) ? oEvent.keyCode : 0;<br />

oEvent.eventPhase = 2;<br />

}<br />

return oEvent;<br />

};<br />

Next in the table is the isChar property, which is true if the charCode property is not 0:<br />

EventUtil.formatEvent = function (oEvent) {<br />

if (isIE && isWin) {<br />

oEvent.charCode = (oEvent.type == “keypress”) ? oEvent.keyCode : 0;<br />

oEvent.eventPhase = 2;<br />

oEvent.isChar = (oEvent.charCode > 0);<br />

}<br />

return oEvent;<br />

};<br />

The keyCode property is the same in both browsers, and the metaKey property cannot be recreated in<br />

IE, so that brings up pageX and pageY. Although the IE event object doesn’t have equivalent properties,<br />

these properties can be calculated by taking the clientX and clientY values and augmenting them<br />

with the scrollLeft and scrollTop values of the document body:<br />

EventUtil.formatEvent = function (oEvent) {<br />

if (isIE && isWin) {<br />

oEvent.charCode = (oEvent.type == “keypress”) ? oEvent.keyCode : 0;<br />

oEvent.eventPhase = 2;<br />

oEvent.isChar = (oEvent.charCode > 0);<br />

oEvent.pageX = oEvent.clientX + document.body.scrollLeft;<br />

oEvent.pageY = oEvent.clientY + document.body.scrollTop;<br />

296

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!