Skip to content

Instantly share code, notes, and snippets.

@lennybacon
Last active August 29, 2015 14:17
Show Gist options
  • Save lennybacon/7c9ebfce7a3a64b7ec97 to your computer and use it in GitHub Desktop.
Save lennybacon/7c9ebfce7a3a64b7ec97 to your computer and use it in GitHub Desktop.
Attach an event
function bindEvent(el, eventName, eventHandler, bubble) {
var handler;
// http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
// http://www.quirksmode.org/js/events_order.html#link9
handler =
function (e) {
if (!bubble) {
if (!e) e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
eventHandler(e);
};
if (el.addEventListener) {
el.addEventListener(eventName, handler, !bubble);
} else if (el.attachEvent) {
el.attachEvent('on' + eventName, handler);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment