Chapter 6
The three approaches are inline, using syntax such as
onloadon thebodyelement; using the traditional DOM Level 0 event capturing, such aswindow.onload; and using the newer DOM Level 2 events, such asaddEventListenerorattachEvent.If using the DOM Level 0 event-handling system, you either access the event object on the window object or passed in as a function. For DOM Level 2, the event object is always passed into the function. From the event object, access the
screenXorscreenYproperties.The IE approach differs from that supported by most browsers, and as such, you have to support both it and the others. Test if the
stopPropagationmethod is supported on the event object and if so, invoke it; otherwise, set thecancelBubbleproperty totrue.The answer is:
if (window.addEventListener) { window.addEventListener("load",functionCall,false); } else if (window.attachEvent) { window.attachEvent("onload", functionCall); }Though we havenât covered capturing keyboard events, typically you capture the
keydownevent and then access the Unicode key code from thewhichproperty on the event:if (document.addEventListener) { document.addEventListener("keydown",getKey,true); } else if (document.attachEvent) { document.attachEvent("onkeydown", getKey); } function getKey(evnt) { alert(evnt.which); }
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access