Cross Browser Add Event Listener


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.