Add Event Listener


/ Published in: JavaScript

Cross-browser implementation of element.addEventListener(). Simple, straightforward, and functional.

Useage:
addListener(window, 'load', myFunction);


Copy this code and paste it in your HTML
  1. // Cross-browser implementation of element.addEventListener()
  2. function addListener(element, type, expression, bubbling)
  3. {
  4. bubbling = bubbling || false;
  5.  
  6. if(window.addEventListener) { // Standard
  7. element.addEventListener(type, expression, bubbling);
  8. return true;
  9. } else if(window.attachEvent) { // IE
  10. element.attachEvent('on' + type, expression);
  11. return true;
  12. } else return false;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.