title: Sys.Application.add_load() vs. $(document).ready() vs. pageLoad()
body: <p>I have page that has some javascript that needs to run at page load.  Said javascript needs to locate the client-side component of a ServerControl, which it does with $find().</p>
  
  <p>Of course, if I emit my code directly onto the page, it executes as the page is being read, and fails because nothing it depends on is yet initialized.</p>
  
  <p>If I put my code inside a pageLoad() function, it runs just fine, because asp.net automatically wires up an onload handler for any function named pageLoad().  The problem is I really don't like the pageLoad() solution - mainly because it's a single global name.  If I commit some code using pageLoad(), I just know that some other programmer is going to copy the approach, somewhere inappropriate, and we're going to end up with a page that includes two or more different pageLoad() functions, and the result will be a bunch of mysterious errors that will take forever to track down.</p>
  
  <p>So, I put my code inside an anonymous function passed to jquery's $(document).ready().  This fails, because it runs before the ServerControl's client-side component exists.</p>
  
  <p>So, I put my code inside an anonymous function passed to by Sys.Application.add_load().  This also fails, because Sys is undefined. </p>
  
  <p>So I finally settle on putting my code inside Sys.Application.add_load(), and then put that inside a function called by $(document).ready().  This works, but it gives nearly as much heartburn as pageLoad().</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function(){
          Sys.Application.add_load(function(){
              var component = $find(&lt;clientid&gt;);
              if (component) { &lt;do something&gt; }
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>There has to be a better way of handling this.</p>
  
  <p>Any ideas?</p>