Created
May 10, 2014 07:09
-
-
Save ChaseWest/cf464463b82b24999ff3 to your computer and use it in GitHub Desktop.
Generic pure js function to attach event listeners to DOM nodes
This file contains 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
var is_attachEvent = !!document.attachEvent, | |
is_addEventListener = !!document.addEventListener; | |
function attach(target, event, fn){ | |
switch(true){ | |
case is_addEventListener: | |
target.addEventListener(event, fn); | |
break; | |
case is_attachEvent: | |
target.attachEvent("on"+event, fn); | |
break; | |
default: | |
target["on" + event] = fn; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment