title: What is the best way to programmatically run javascript when an ASP.net page loads?
body: <p>In my <code>global.asax</code> file for my ASP.net project, I am checking for certain conditions.  When those conditions are met, I want to automatically execute javascript code when the page runs.</p>
  
  <p>This is my code:</p>
  
  <blockquote>
    <p>if condition  Then<br>
        Response.Write(" &lt; script type=""text/javascript"" > ")<br>
        Response.Write(" // Javascript code to do stuff ")<br>
        Response.Write(" &lt; /script > ")<br>
    End If</p>
  </blockquote>
  
  <p>While this appears to work to execute the Javascript code, I don't think it's a best practice because this code will preceed <em>all</em> of the HTML of the page that gets loaded.</p>
  
  <p>What is the best way of programmatically tacking on some extra Javascript code to be run when my page loads?</p>
  
  <p><b>Update</b> Thanks for the answer.  Too bad this solution doesn't work from within <code>global.asax</code>.  Is there no way to make this happen site-wide?  It seems like <code>global.asax</code> would be the logical place to put code that runs with every page...  <code>Response.Write</code> works fine in <code>global.asax</code>.</p>