Created
July 6, 2011 12:37
-
-
Save abernier/1067125 to your computer and use it in GitHub Desktop.
Cross-browsers events listener implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
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