Skip to content

Instantly share code, notes, and snippets.

@hwclass
Last active August 29, 2015 14:01
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 hwclass/2fe18e87d0c6a84f0c5d to your computer and use it in GitHub Desktop.
Save hwclass/2fe18e87d0c6a84f0c5d to your computer and use it in GitHub Desktop.
/**
* addEvent() controls event-handling for cross-browser issues
*
* @param <Array> elem
* @param <Array> event
* @param <Function> fn
*
*/
var app = app || {};
app = {
utils : {
addEvent : function (elem, event, fn) {
function listenHandler(e) {
var ret = fn.apply(this, arguments);
if (ret === false) {
e.stopPropagation();
e.preventDefault();
}
return(ret);
}
if (elem.addEventListener) {
elem.addEventListener(event, listenHandler, false);
} else if (elem.attachEvent) {
elem.attachEvent ("on" + type, fn);
} else {
elem.attachEvent("on" + event, attachHandler);
}
}
},
fn : {
handlerClick : function () {
alert('Button clicked...');
}
},
init : function () {
app.utils.addEvent(document.getElementById('newButton'), 'click', app.fn.handlerClick);
}
}
app.init();
@hwclass
Copy link
Author

hwclass commented May 28, 2014

Cross-browser event handling for any purpose to understand clearly how libraries like jQuery works for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment