This section provides a tutorial example of a JavaScript code inserts another JavaScript code back into the HTML document. The Web browser executes script code multiple times.
As we learned earlier, when the Web browser receives the HTML document from the Web server,
it will remove all embedded script codes and execute them.
What happens if a new script code is inserted back into the HTML document during the first round of script execution?
The Web browser will remove the inserted script code and execute it again.
Here is an interesting tutorial example of a JavaScript code that inserts another JavaScript code into the HTML document:
<html>
<!-- Hello_Nested_Scripts.html
Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head><title>Hello from Nested Scripts</title></head>
<body><pre>
<script type="text/javascript">
document.writeln("Hello World!");
document.writeln("<script type=\"text/javascript\">");
document.writeln("document.writeln(\" Hello World!\");");
document.writeln("<\/script>");
</script>
</pre></body>
</html>
Take a guess on what you will see on the browser window if you run this HTML document.
Hello World!
Hello World!
What happens is that when the browser finish the execution of the embedded JavaScript code,
it will get the following the HTML document: