-
-
Save fj-auto/16b5e457927f769186ef to your computer and use it in GitHub Desktop.
JS | Cross Browsers Event Utility Object
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
var eventUtility = { | |
addEvent : function(el, type, fn) { | |
if (typeof addEventListener !== "undefined") { | |
el.addEventListener(type, fn, false); | |
} else if (typeof attachEvent !== "undefined") { | |
el.attachEvent("on" + type, fn); | |
} else { | |
el["on" + type] = fn; | |
} | |
}, | |
removeEvent : function(el, type, fn) { | |
if (typeof removeEventListener !== "undefined") { | |
el.removeEventListener(type, fn, false); | |
} else if (typeof detachEvent !== "undefined") { | |
el.detachEvent("on" + type, fn); | |
} else { | |
el["on" + type] = null; | |
} | |
}, | |
getTarget : function(event) { | |
if (typeof event.target !== "undefined") { | |
return event.target; | |
} else { | |
return event.srcElement; | |
} | |
}, | |
preventDefault : function(event) { | |
if (typeof event.preventDefault !== "undefined") { | |
event.preventDefault(); | |
} else { | |
event.returnValue = false; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment