We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

rhlowe on 07/02/07


Tagged


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

noah


Cross Browser Add Event Listener


Published in: JavaScript 


Snatched this from another post and fixed something I found was not working.

  1. // Cross-browser implementation of element.addEventListener()
  2.  
  3. function listen(evnt, elem, func) {
  4. if (elem.addEventListener) // W3C DOM
  5. elem.addEventListener(evnt,func,false);
  6. else if (elem.attachEvent) { // IE DOM
  7. var r = elem.attachEvent("on"+evnt, func);
  8. return r;
  9. }
  10. else window.alert('I\'m sorry Dave, I\'m afraid I can\'t do that.');
  11. }
  12.  
  13. // Use: listen("event name", elem, func);

Report this snippet 

You need to login to post a comment.