Skip to content

Instantly share code, notes, and snippets.

@RyoSugimoto
Created April 22, 2015 00:53
Show Gist options
  • Save RyoSugimoto/6ef7cc69506ef5d5f8cc to your computer and use it in GitHub Desktop.
Save RyoSugimoto/6ef7cc69506ef5d5f8cc to your computer and use it in GitHub Desktop.
イベントリスナを追加・削除する(旧IE対応)。
var addEvent,
removeEvent;
if (typeof window.addEventListener === 'function') {
addEvent = function (el, type, fn) { el.addEventListener(type, fn, false); };
removeEvent = function (el, type, fn) { el.removeEventListener(type, fn, false); };
} else if (typeof document.attachEvent === 'function') {
addEvent = function (el, type, fn) { el.attachEvent('on' + type, fn); };
removeEvent = function (el, type, fn) { el.detachEvent('on' + type, fn); };
} else {
addEvent = function (el, type, fn) { el['on' + type] = fn; };
removeEvent = function (el, type, fn) { el['on' + type] = null; };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment