Skip to content

Instantly share code, notes, and snippets.

@abernier
Created July 6, 2011 12:37
Show Gist options
  • Save abernier/1067125 to your computer and use it in GitHub Desktop.
Save abernier/1067125 to your computer and use it in GitHub Desktop.
Cross-browsers events listener implementation
({
addListener: null,
removeListener: null,
init: function () {
if (typeof window.addEventListener === 'function') {
this.addListener = function (el, type, fn) {
el.addEventListener(type, fn, false);
};
this.removeListener = function (el, type, fn) {
el.removeEventListener(type, fn, false);
};
} else if (typeof document.attachEvent === 'function') { // IE
this.addListener = function (el, type, fn) {
el.attachEvent('on' + type, fn);
};
this.removeListener = function (el, type, fn) {
el.detachEvent('on' + type, fn);
};
} else { // older browsers
this.addListener = function (el, type, fn) {
el['on' + type] = fn;
};
this.removeListener = function (el, type, fn) {
el['on' + type] = null;
};
};
return this;
}
}).init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment