Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dead simple wrapper for event functions in JavaScript. Adds addListener and removeListener to the global namespace.
(function(w) {
// Create the Event Function wrappers
if (w.addEventListener) {
// standards compliant method
w.addListener = function(el, eType, fn, capture) {
el.addEventListener(eType, fn, capture);
};
w.removeListener = function (el, eType, fn, capture) {
el.removeEventListener(eType, fn, capture);
};
}
else if (w.attachEvent) {
// microsoft compliant method
w.addListener = function(el, eType, fn, capture) {
el.attachEvent("on" + eType, fn, capture);
};
w.removeListener = function (el, eType, fn, capture) {
el.detachEvent("on" + eType, fn, capture);
};
}
else {
// really old browses, circa 1990's
throw('Event handlers are unsupported in this browser.');
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.