Why is code after document.write() not executed?

javascript

Solution

`document.write` can only be used during the initial loading of the document.

If you want to insert your H1 when the function is called, you may replace

document.write("<h1> Just a javascript demo</h1>");

with

var h1 = document.createElement('h1');
h1.innerHTML = " Just a javascript demo";
document.body.appendChild(h1);

Problem

I have the follwing JavaScript. ``` <html> <head> <script language="JavaScript"> function fdivisible() { document.write("<h1> Just a javascript demo</h1>"); var x=document.forms["aaa"]["txt1"].value; alert(x); } </script> </head> <body> <form action="#" name="aaa"> Enter a no. : <input type="text" name="txt1" id="txt1" /> <input type="button" value="Click" onclick="fdivisible();"> </form> </body> </html> ``` The problem is, the first line of the JS function is executing and the rest are ignored. If I comment out the first line the rest of the code is executed. Can anybody explain to me why it is so?

Original source