Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Javascript Events
function (ev) {
e = ev || window.event;
var target = e.target || e.srcElement;
// have fun here
// cancel bubbling?
e.cancelBubble = true;
if(e.stopPropagation) {
e.stopPropagation();
}
// prevent default browser action?
e.returnValue = false;
if(e.preventDefault) {
e.preventDefault();
}
return false;
}
var EventHandler = {
bind:function(el, ev, fn){
if(window.addEventListener){ // modern browsers including IE9+
el.addEventListener(ev, fn, false);
} else if(window.attachEvent) { // IE8 and below
el.attachEvent('on' + ev, fn);
} else {
el['on' + ev] = fn;
}
},
unbind:function(el, ev, fn){
if(window.removeEventListener){
el.removeEventListener(ev, fn, false);
} else if(window.detachEvent) {
el.detachEvent('on' + ev, fn);
} else {
elem['on' + ev] = null;
}
},
stop:function(ev) {
var e = ev || window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment