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 HerongYang.com. All Rights Reserved.
-->
<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 finishes 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.

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

Server-Side and Client-Side Web Scripting

 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

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 AJAX (Asynchronous JavaScript And XML)

 References

 Full Version in PDF/EPUB