Posted By

mifly on 04/22/09


Tagged

javascript DOM


Versions (?)

domready


 / Published in: JavaScript
 

  1. function domReady( f ) {
  2. // If the DOM is already loaded, execute the function right away
  3. if ( domReady.done ) return f();
  4.  
  5. // If we’ve already added a function
  6. if ( domReady.timer ) {
  7. // Add it to the list of functions to execute
  8. domReady.ready.push( f );
  9. } else {
  10. // Attach an event for when the page finishes loading,
  11. // just in case it finishes first. Uses addEvent.
  12. addEvent( window, "load", isDOMReady );
  13.  
  14. // Initialize the array of functions to execute
  15. domReady.ready = [ f ];
  16.  
  17. // Check to see if the DOM is ready as quickly as possible
  18. domReady.timer = setInterval( isDOMReady, 13 );
  19. }
  20. }
  21.  
  22. // Checks to see if the DOM is ready for navigation
  23. function isDOMReady() {
  24. // If we already figured out that the page is ready, ignore
  25. if ( domReady.done ) return false;
  26.  
  27. // Check to see if a number of functions and elements are
  28. // able to be accessed
  29. if ( document && document.getElementsByTagName &&
  30. document.getElementById && document.body ) {
  31.  
  32. // If they’re ready, we can stop checking
  33. clearInterval( domReady.timer );
  34. domReady.timer = null;
  35.  
  36. // Execute all the functions that were waiting
  37. for ( var i = 0; i < domReady.ready.length; i++ )
  38. domReady.ready[i]();
  39.  
  40. // Remember that we’re now done
  41. domReady.ready = null;
  42. domReady.done = true;
  43. }
  44. }

Report this snippet  

You need to login to post a comment.