Posted By

Butscharoni on 10/16/07


Tagged

fix


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


Onload Fix


 / Published in: JavaScript
 

URL: http://dean.edwards.name/weblog/2006/06/again/

Fixes Onload so that script gets executed as soon as the DOM is complete

  1. // Dean Edwards/Matthias Miller/John Resig
  2.  
  3. function init() {
  4. // quit if this function has already been called
  5. if (arguments.callee.done) return;
  6.  
  7. // flag this function so we don't do the same thing twice
  8. arguments.callee.done = true;
  9.  
  10. // kill the timer
  11. if (_timer) clearInterval(_timer);
  12.  
  13. // do stuff
  14. };
  15.  
  16. /* for Mozilla/Opera9 */
  17. if (document.addEventListener) {
  18. document.addEventListener("DOMContentLoaded", init, false);
  19. }
  20.  
  21. /* for Internet Explorer */
  22. /*@cc_on @*/
  23. /*@if (@_win32)
  24.   document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  25.   var script = document.getElementById("__ie_onload");
  26.   script.onreadystatechange = function() {
  27.   if (this.readyState == "complete") {
  28.   init(); // call the onload handler
  29.   }
  30.   };
  31. /*@end @*/
  32.  
  33. /* for Safari */
  34. if (/WebKit/i.test(navigator.userAgent)) { // sniff
  35. var _timer = setInterval(function() {
  36. if (/loaded|complete/.test(document.readyState)) {
  37. init(); // call the onload handler
  38. }
  39. }, 10);
  40. }
  41.  
  42. /* for other browsers */
  43. window.onload = init;

Report this snippet  

You need to login to post a comment.