Posted By

maceblue on 07/30/12


Tagged

events Handling cross-browser


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

pibot77


adding events browser compatible


 / Published in: JavaScript
 

events handling cross-browser

  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  

You need to login to post a comment.