views:

764

answers:

2

Hello. In Google Chrome, the following code snippet:

<html xmlns = "http://www.w3.org/1999/xhtml"&gt;
    <head>
     <title>Example</title>    
     <script type = "text/javascript">
     <!--
      function onBodyLoad()
      {    
       var x = 42;
       document.writeln("x = " + x);
       alert("on load");
      }
     -->
     </script>
    </head>

    <body onload = "onBodyLoad()"></body>
</html>

Is not writing

x = 42

to the document. The alert however is displayed. The text and the alert are both displayed in IE8 and FF 3.5. Any thoughts?

+1  A: 

Edit: Check and see if this post helps. I dont use chrome and you have to add document.close();

; missing in document.writeln("x = " + x) (?)

Shoban
Oops, that was just a typo in my submission, the ; is there in the original source.
Raul Agrait
No it wasn't. Someone added it after your post.
Martin Sturm
+2  A: 

You have to do a document.close() after document.writeln():

            function onBodyLoad()
            {                               
                    var x = 42;
                    document.writeln("x = " + x);
                    alert("on load");
                    document.close();
            }

See: this question as well.

Martin Sturm