Last active
August 29, 2015 14:01
-
-
Save hwclass/2fe18e87d0c6a84f0c5d 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
/** | |
* addEvent() controls event-handling for cross-browser issues | |
* | |
* @param <Array> elem | |
* @param <Array> event | |
* @param <Function> fn | |
* | |
*/ | |
var app = app || {}; | |
app = { | |
utils : { | |
addEvent : function (elem, event, fn) { | |
function listenHandler(e) { | |
var ret = fn.apply(this, arguments); | |
if (ret === false) { | |
e.stopPropagation(); | |
e.preventDefault(); | |
} | |
return(ret); | |
} | |
if (elem.addEventListener) { | |
elem.addEventListener(event, listenHandler, false); | |
} else if (elem.attachEvent) { | |
elem.attachEvent ("on" + type, fn); | |
} else { | |
elem.attachEvent("on" + event, attachHandler); | |
} | |
} | |
}, | |
fn : { | |
handlerClick : function () { | |
alert('Button clicked...'); | |
} | |
}, | |
init : function () { | |
app.utils.addEvent(document.getElementById('newButton'), 'click', app.fn.handlerClick); | |
} | |
} | |
app.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cross-browser event handling for any purpose to understand clearly how libraries like jQuery works for it.