Why do I need RegisterStartupScript for a simple jquery helloworld in asp.net?
<p>I tried to call my helloworld by just including javascript inside webform but when running it page is blank on both chrome and firefox. In firefox error is</p>

<blockquote>
  <p>"XML Parsing Error: no element found"</p>
</blockquote>

<pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jquery01._Default" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
    &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt; 
        &lt;/script&gt;
&lt;script type="text/javascript"&gt;
    function helloWorld() {
        $("#divSample").append("Hello World!!");
    }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div id="divSample"&gt;

    &lt;/div&gt;
&lt;script type="text/javascript"&gt;    helloWorld();&lt;/script&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>I needed to add in codebehind:</p>

<pre><code>protected override void  Render(HtmlTextWriter writer)
{

    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(),
        "startup", "&lt;script type=\"text/javascript\"&gt;helloWorld();&lt;/script&gt;");
    base.Render(writer);
}
</code></pre>

<p>In that case it works but I don't understand why I just can't use the 1st syntax why it's so complicated for such a simple stuff ?</p>

<p>I also tried the suggestion but it didn't work either:</p>

<pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jquery01._Default" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
    &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt; 
        &lt;/script&gt;
&lt;script type="text/javascript"&gt;
    function helloWorld() {
        $("#divSample").append("Hello World!!");
    }
    &lt;/script&gt;
&lt;script type="text/javascript"&gt;
    $(document).ready(function () {
        helloWorld();
    });
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div id="divSample"&gt;

    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Update: Seems ASP.NET can be unreliable in some circumstances with ajax / jquery ?
<a href="http://chiragrdarji.wordpress.com/2010/02/17/xml-parsing-error-no-element-found/" rel="nofollow">http://chiragrdarji.wordpress.com/2010/02/17/xml-parsing-error-no-element-found/</a></p>
