Created
February 28, 2011 06:09
-
-
Save Melipone/846999 to your computer and use it in GitHub Desktop.
Exercises from Chapter 13 of Eloquent Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ex 13.1 of Chapter 13 in Eloquent Javascript | |
// Write a function called registerEventHandler to wrap the | |
// incompatibilities of these two models. It takes three arguments: first | |
// a DOM node that the handler should be attached to, then the name of | |
// the event type, such as "click" or "keypress", and finally the handler | |
// function. | |
function registerEventHandler (node, eventType, handlerFN) { | |
if (node.attachEvent) { //IE | |
node.attachEvent("on"+eventType, handlerFN); | |
else // all others | |
node.addEventListener(eventType, handlerFN, false); | |
} | |
// okay, that works. There might be other ways to check whether a method on a node exists or not | |
// EloquentJavascript solution: | |
//function registerEventHandler(node, event, handler) { | |
// if (typeof node.addEventListener == "function") | |
// node.addEventListener(event, handler, false); | |
// else | |
// node.attachEvent("on" + event, handler); | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment