Posted By

sarfraznawaz2005 on 02/11/09


Tagged

javascript event load


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jfherring
jafg
Anothnio


Great Function


 / Published in: JavaScript
 

Ever wanted to add multiple functions to the onload event rather than one? This function lets you achieve exactly that.

  1. /*
  2. This function lets you add multiple functions to the Load event of the document.
  3.  
  4. Example Usage :
  5. addLoadListener(firstFunction);
  6. addLoadListener(secondFunction);
  7. addLoadListener(twentyThirdFunction);
  8. */
  9.  
  10. function addLoadListener(fn)
  11. {
  12. if (typeof window.addEventListener != 'undefined')
  13. {
  14. window.addEventListener('load', fn, false);
  15. }
  16. else if (typeof document.addEventListener != 'undefined')
  17. {
  18. document.addEventListener('load', fn, false);
  19. }
  20. else if (typeof window.attachEvent != 'undefined')
  21. {
  22. window.attachEvent('onload', fn);
  23. }
  24. else
  25. {
  26. var oldfn = window.onload;
  27. if (typeof window.onload != 'function')
  28. {
  29. window.onload = fn;
  30. }
  31. else
  32. {
  33. window.onload = function()
  34. {
  35. oldfn();
  36. fn();
  37. };
  38. }
  39. }
  40. }

Report this snippet  

You need to login to post a comment.