Skip to content

Instantly share code, notes, and snippets.

@ykhs
Created June 27, 2011 12:49
Show Gist options
  • Save ykhs/1048792 to your computer and use it in GitHub Desktop.
Save ykhs/1048792 to your computer and use it in GitHub Desktop.
オレオレ addEvent
function addEvent(element, type, handler, data, context) {
if (!element) { return false; }
if (!handler) { return false; }
context = context || element;
var fn = function (e) {
e = e || win.event;
if (!e.target) {
e.target = e.srcElement;
}
if (!e.stopPropatation) {
e.stopPropagation = function () {
e.cancelBubble = true;
};
}
if (!e.preventDefault) {
e.preventDefault = function () {
e.returnValue = false;
};
}
e && (e.data_obj = data);
if (handler.call(context, e) === false) {
e.stopPropagation();
e.preventDefault();
}
}
if (element.addEventListener) {
element.addEventListener(type, fn, false);
} else if (element.attachEvent) {
element.attachEvent('on' + type, fn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment