Skip to content

Instantly share code, notes, and snippets.

@Melipone
Created February 28, 2011 06:09
Show Gist options
  • Save Melipone/846999 to your computer and use it in GitHub Desktop.
Save Melipone/846999 to your computer and use it in GitHub Desktop.
Exercises from Chapter 13 of Eloquent Javascript
// 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