Skip to content

Instantly share code, notes, and snippets.

@ChaseWest
Created May 10, 2014 07:09
Show Gist options
  • Save ChaseWest/cf464463b82b24999ff3 to your computer and use it in GitHub Desktop.
Save ChaseWest/cf464463b82b24999ff3 to your computer and use it in GitHub Desktop.
Generic pure js function to attach event listeners to DOM nodes
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