Last active
August 29, 2015 14:22
-
-
Save raindrop-ua/afbb28dc55bfcbffc64a to your computer and use it in GitHub Desktop.
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
function addEvent(elem, event, fn) { | |
function listenHandler(e) { | |
var ret = fn.apply(this, arguments); | |
if (ret === false) { | |
e.stopPropagation(); | |
e.preventDefault(); | |
} | |
return(ret); | |
} | |
function attachHandler() { | |
var ret = fn.call(elem, window.event); | |
if (ret === false) { | |
window.event.returnValue = false; | |
window.event.cancelBubble = true; | |
} | |
return(ret); | |
} | |
if (elem.addEventListener) { | |
elem.addEventListener(event, listenHandler, false); | |
} else { | |
elem.attachEvent("on" + event, attachHandler); | |
} | |
} | |
// Simpler version without the propagation and default prevention options but with this and event normalization: | |
function addEvent(elem, event, fn) { | |
if (elem.addEventListener) { | |
elem.addEventListener(event, fn, false); | |
} else { | |
elem.attachEvent("on" + event, function() { | |
return(fn.call(elem, window.event)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment