adding events browser compatible


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

events handling cross-browser


Copy this code and paste it in your HTML
  1. function addEvent(elm, evType, fn, useCapture) {
  2. if (elm.addEventListener) {
  3. elm.addEventListener(evType, fn, useCapture);
  4. return true;
  5. }
  6. else if (elm.attachEvent) {
  7. var r = elm.attachEvent('on' + evType, fn);
  8. return r;
  9. }
  10. else {
  11. elm['on' + evType] = fn;
  12. }
  13. }
  14.  
  15. function addLoadEvent(func) {
  16. var oldonload = window.onload;
  17. if (typeof window.onload != 'function') {
  18. window.onload = func;
  19. }
  20. else {
  21. window.onload = function() {
  22. oldonload();
  23. func();
  24. }
  25. }
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.