title: Javascript - How to detect if document has loaded (IE 7/Firefox 3)
body: <p>I want to call a function after a document loads, but the document may or may not have finished loading yet.  If it did load, then I can just call the function. If it did NOT load, then I can attach an event listener. I can't add an eventlistener after onload has already fired since it won't get called.  So how can I check if the document has loaded?  I tried the code below but it doesn't entirely work.  Any ideas?</p>
  
  <pre><code>var body = document.getElementsByTagName('BODY')[0];
  // CONDITION DOES NOT WORK
  if (body &amp;&amp; body.readyState == 'loaded') {
      DoStuffFunction();
  } else {
      // CODE BELOW WORKS
      if (window.addEventListener) {  
          window.addEventListener('load', DoStuffFunction, false);
      } else {
          window.attachEvent('onload', DoStuffFunction);
      }
  }
  </code></pre>