Skip to content

Instantly share code, notes, and snippets.

@netroy
Created May 15, 2011 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netroy/973062 to your computer and use it in GitHub Desktop.
Save netroy/973062 to your computer and use it in GitHub Desktop.
Better AttachEvent
function(c,a,e,b){function d(a){e.apply(c,[a||event].concat(b))}b=b||[];try{c.addEventListener(a,d,!1)}catch(f){c.attachEvent("on"+a,d)}};
function addEvent(
node, // Node to attach event to
eventName, // Event Name
handler, // Callback for event
args){ // Extra arguments to pass to handler (default none)
args = args || [];
function callback(e){
e = e||event;
e.target = e.target||e.srcElement;
handler.apply(node,[e].concat(args))
}
if (node.addEventListener){
node.addEventListener(eventName, callback, false);
} else if (node.attachEvent){
node.attachEvent('on'+eventName, callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment