Skip to content

Instantly share code, notes, and snippets.

@jCrip
Created March 15, 2012 14:22
Show Gist options
  • Save jCrip/2044435 to your computer and use it in GitHub Desktop.
Save jCrip/2044435 to your computer and use it in GitHub Desktop.
Modern Event Handling
<script type="text/javascript">
/**
* Attach an event handler on a given Node taking care of Browsers Differences
* @param {Object} node
* @param {String} type
* @param {Function} fn
* @param {Boolean} capture
*/
function addEventHandler(node,type,fn , capture){
if(typeof window.event !== "undefined"){
/* Internet Explorer way */
node.attachEvent( "on" + type, fn );
} else {
/* FF & Other Browsers */
node.addEventListener( type, fn , capture );
}
}
/* Example */
addEventHandler(window,"load",function(){
alert("The page was loaded");
},true)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment