title: Javascript to display sharepoint list items on page load
body: <p>With Client Object Model API and a bit of javascript, I want to retrieve the items of a sharepoint list and display them into the web page, <strong>when the web page loads</strong>.</p>
  
  <p>I've successfully implemented examples working with button or link click event handler. But when I try to use the code with <code>onload</code> I get an error.</p>
  
  <hr>
  
  <p>Assuming I've my <code>ViewItem()</code> function defined in the head.</p>
  
  <p>If I use this code in the body:</p>
  
  <pre><code>  &lt;a onclick="javascript:ViewItem()"&gt;View&lt;/a&gt;
  </code></pre>
  
  <p>all is fine.</p>
  
  <p>If I try with classic <code>onlaod</code>, the call fails:</p>
  
  <pre><code> &lt;script type="text/javascript"&gt;
    window.onload = ViewItem ;
   &lt;/script&gt;
  </code></pre>
  
  <hr>
  
  <p>The line of code in <code>ViewItem</code> function which generates the error is as follows:</p>
  
  <pre><code>   var myContext = new SP.ClientContext.get_current();
  </code></pre>
  
  <p>This code works fine with the onclick event handler, but fails with the onload event handler with error:</p>
  
  <blockquote>
    <blockquote>
      <p><em>SP.ClientContext is null or not an object</em></p>
    </blockquote>
  </blockquote>
  
  <p>How can I solve this issue? Why it happens only when the function is called with <code>onload</code>?</p>