JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.11

Client-Side Scripting Processed Multiple Times

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:

Hello World!
<script type="text/javascript">
document.writeln(" Hello World!");
</script>

There is another JavaScript code in the output. The browser will execute it again resulting the final HTML document you saw earlier.

Sections in This Chapter

Web Scripting Architecture Overview

Server-Side Scripting Overview

Client-Side Scripts for Document Updating

Client-Side Scripts for Event Handling

Client-Side Scripting Processed Multiple Times

New Script Resulted from Two Original Scripts

Dr. Herong Yang, updated in 2008
Client-Side Scripting Processed Multiple Times