Skip to content

Instantly share code, notes, and snippets.

@mattsnider
Created February 15, 2013 23:24
Show Gist options
  • Save mattsnider/4964452 to your computer and use it in GitHub Desktop.
Save mattsnider/4964452 to your computer and use it in GitHub Desktop.
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