title: jquery document ready equivalent in Javascript?
body: <p>i want to put my javascript code in head instead of body, but because the head loads before the body section in the html document, my code becomes useless as all selectors become undefined.</p>
  
  <p>in jquery i can use <code>$(document).ready</code> to overcome this problem, however i dont want to use jquery and i'm wondering if there's a solution for this in javascript.</p>
  
  <p>i tried:</p>
  
  <pre><code>if(document.readyState === "complete") {/*my javascript code */}
  </code></pre>
  
  <p>and</p>
  
  <pre><code>window.onload = function(){/*my javascript code */}
  </code></pre>
  
  <p>but both doesn't work, any suggestions? thanks in advance.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div id="section" onClick="shout()"&gt;test&lt;/div&gt;
  </code></pre>
  
  <p>JAVASCRIPT:</p>
  
  <pre><code>window.onload=function(){
  function shout(){
  var str = document.getElementById("section").innerHTML;
  alert(str);
  }
  }
  </code></pre>