title: Hooking GET variables into onload JavaScript events
body: <p>Using C# .NET 3.5 Framework here to make an <code>.aspx</code> web page.  Anyways, the page accepts a <code>GET</code> variable <code>x</code>, and if supplied, I want the page to hook a JavaScript function <code>foo(x)</code> to the <code>onload</code> event of the page.  The <code>GET</code> variable must be passed as an argument to <code>foo</code>.</p>
  
  <p>I'm more familiar with other server-side scripting languages, and for example, in php I might do something like the following snippet, where I embed the php right into the <code>&lt;head&gt;</code> of the html page in the <code>&lt;script&gt;</code> (for simplicity I just set foo to the <code>window.onload</code> as I probably will only ever have one function set to this event anyways):</p>
  
  <pre><code>...
  
  &lt;script language="javascript" type="text/javascript"&gt;
  function foo(x) {
      alert( x );
  }
  
  &lt;?php
  
  if( isset($_GET["x"]) )
      echo "window.onload = foo('" . $_GET["x"] . "');";
  ?&gt;
  &lt;/script&gt;
  
  ...
  </code></pre>
  
  <p>Alternatively, I make the <code>.js</code> file a php parsed file that outputs the same and link that from the html file via <code>src</code> attribute of <code>&lt;script&gt;</code> (but I would still need a bit of embedded PHP to pass the <code>GET</code> variable to that script file <code>src</code> URL). </p>
  
  <p>Anyways, with this <code>aspx</code> stuff, it looks a lot different, in that I have an underlying <code>.cs</code> file and there seems to be a huge strive to separate logic like this from the content of the page (.aspx) file.  I wasn't really sure how to get this same kind of behavior in this kind of environment.</p>
  
  <p>It's kind of not really ideal to have my JavaScript source inside my C# source as a string, etc.</p>
  
  <p>Anyways, I did some searches on this but I can't really find a clean solution.  Anyone have a sensible solution for this type of thing in .aspx?</p>
  
  <p>I should also point out that I do not want to rely on JavaScript to parse the <code>window.location</code> for the <code>GET</code> variables, as I use extensive <em>path rewriting</em> and I want to be flexible for <code>POST</code> variables.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Ok, so using the answers so far that are more focused on asp side of things I can mimic the php flavor as close as possible with something like this:</p>
  
  <pre><code>...
      &lt;script language="javascript" type="text/javascript"&gt;
  
      function foo(x) {
          alert( x );
      }
  
  
      &lt;%    
         String x = this.Request["x"];
         if(!String.IsNullOrEmpty(x))
             Response.Write( "window.onload = foo('" + Server.UrlDecode(x) + "');" );
  
      %&gt;
  &lt;/script&gt;
  ...
  </code></pre>
  
  <p>Though this works, it's not exactly the answer I am looking for.  I was hoping for an aspx.cs solution that would <em>divorce the need to embed</em> this kind of code in my html file.  Perhaps I was misleading when I gave the PHP example.  I'd like to handle this on the .cs side of the world rather than embedding it directly in the html of the aspx file.  Any ideas?</p>