title: Javascript: Detect when a window is fully loaded
body: <p>I have a script which may be loaded at any stage of the life cycle of a web page.
  When the script is loaded it must run an initialize() method.</p>
  
  <p>I want this function to be run on the "onload" event, but I cannot be certain that the page has not already loaded, i.e. that the "onload" has not been fired already.</p>
  
  <p>Ideally my script would look like this:</p>
  
  <pre><code>var _initialize = function() { ...};
  
  if(window.LOADED)
      _initialize();
  else if (window.addEventListener)
      window.addEventListener('load', _initialize, false); 
  else if (window.attachEvent)
      window.attachEvent('onload', _initialize);
  </code></pre>
  
  <p>Is there any such window.LOADED or document.LOADED variable or something similar?</p>
  
  <p>Thanks in advance,
  Shane</p>