Posted By

mjenos on 04/06/12


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

octaviannitagmailcom
spee66


Modern Event Handling


 / Published in: JavaScript
 

  1. <script type="text/javascript">
  2. /**
  3.  * Attach an event handler on a given Node taking care of Browsers Differences
  4.  * @param {Object} node
  5.  * @param {String} type
  6.  * @param {Function} fn
  7.  * @param {Boolean} capture
  8.  */
  9. function addEventHandler(node,type,fn , capture){
  10. if(typeof window.event !== "undefined"){
  11. /* Internet Explorer way */
  12. node.attachEvent( "on" + type, fn );
  13. } else {
  14. /* FF & Other Browsers */
  15. node.addEventListener( type, fn , capture );
  16. }
  17. }
  18.  
  19. /* Example */
  20. addEventHandler(window,"load",function(){
  21. alert("The page was loaded");
  22. },true)
  23. </script>
  24.  

Report this snippet  

You need to login to post a comment.