tags:

views:

149

answers:

4

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

"XML Parsing Error: no element found"

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jquery01._Default" %>

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

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

    </div>
<script type="text/javascript">    helloWorld();</script>
    </form>
</body>
</html>

I needed to add in codebehind:

protected override void  Render(HtmlTextWriter writer)
{

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

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 ?

I also tried the suggestion but it didn't work either:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jquery01._Default" %>

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

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

</head>
<body>
    <form id="form1" runat="server">
    <div id="divSample">

    </div>
    </form>
</body>
</html>

Update: Seems ASP.NET can be unreliable in some circumstances with ajax / jquery ? http://chiragrdarji.wordpress.com/2010/02/17/xml-parsing-error-no-element-found/

+1  A: 

Try wrapping your helloWorld() call in jQuery $(document).ready syntax:

<script type="text/javascript">
    $(document).ready(function(){
        helloWorld();
    });
</script>
Nathan Donze
I just tried it doesn't work for me
what kind of error are you getting?
Bryce Fischer
"XML Parsing Error: no element found" the same as the guy here http://chiragrdarji.wordpress.com/2010/02/17/xml-parsing-error-no-element-found/
html source code is completely empty on both google chrome and firefox
As others have stated, this works perfectly for me (IE8, Chrome 6.0.472.63, Firefox 3.6.10).
Nathan Donze
+1  A: 

Have you tried to run it in a debugger, like FireBug for FIreFox or Chrome's developer tools? (Haven't tried IE's yet... I copyied and pasted your code above and it worked fine for me...

Bryce Fischer
I have this error in firefox and found an article here that explains why http://chiragrdarji.wordpress.com/2010/02/17/xml-parsing-error-no-element-found/
I tried his solution but it didn't work either
when displaying your page, view source and take a look at what the generated source is. And could you post that?
Bryce Fischer
Well source is just ... blank (empty) :)
you mean there is no <html><head>... tags in the source at all?
Bryce Fischer
YES, nothing at all because of the browser xml error
that is odd. I created a new web application, copy/pasted your 2nd code example above (the one where you added the $(document).ready ...and ran it. It worked as expected. Unless there's some aspect that we don't know about here, I"m at a loss for why you are getting the results you are.
Bryce Fischer
I think it's a random error which depends on the server see http://stackoverflow.com/questions/3867212/asp-net-ajax-jquery-reliability which can be even random for the same server. that means your code may or may not work that's worrisome.
I tested in IE 7 I just have <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD><BODY></BODY></HTML>
There has to be more to the issue than what you are posting. I have never had any issue with ASP.NET, JQuery and ajax. All issues I did come across always turned out to be bugs in my code. Is there some code behind that we aren't seeing?
Bryce Fischer
+1  A: 

try using a delay="delay" attribute on your script tag.

<script type="text/javascript" delay="delay">helloWorld();</script>

IE does not like if you try to modify the DOM structure when the html is still being rendered. this attribute tells the browser to delay script execution until it is done with the rendering.

Vinay B R
thanks for trial but doesn't change either firefox or IE
A: 

Did you try getting rid of the Render override?

protected override void Render(HtmlTextWriter writer)
{
    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(),
        "startup", "<script type=\"text/javascript\">helloWorld();</script>");
    base.Render(writer);
}
Roadie57