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

Including 'script' Tags in String Literials

This section provides a tutorial example showing the issue of including 'script' tags in string literals in HTML document.

As we learned from the previous section, JavaScript codes must be included in "script" tags in HTML documents like: <script ...> JavaScript code </script>. This put an extra restriction on the JavaScript code:

Never include </script> directly any where in your JavaScript code.

If you use </script> directly in your JavaScript code, it will cause the Web browser or server to end your JavaScript code prematurely and cause execution errors. Here is a tutorial example of including </script> directly:

<html>
<!-- Escape_Script_Tag.html
   Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head><title>Escaping Script Tags</title></head>
<body><pre>
<script type="text/javascript">
   document.writeln("Qeustion: How to use the <script> tag?");
   document.writeln("Answer: <script>...Script Code...</script>");
   document.writeln("Rate this answer: 1 | 2 | 3 | 4 | 5");
</script>
</pre></body>
</html>

If you run this example script in a browser, you will get:

");
   document.writeln("Rate this answer: 1 | 2 | 3 | 4 | 5");

Surprised? Not really. Here is what happened when the browser processes this HTML document:

  • When the browser reaches the </script> tag included in the string literal in the document.writeln() statement, it will end the JavaScript code.
  • The prematurely ended JavaScript code will not write anything to the HTML document, because the last statement is incomplete.
  • The rest of the JavaScript code after the </script> will be processed as static text of the HTML document. This is why it is displayed in the browser window.

See the next section on how to fix this problem.

Table of Contents

 About This JavaScript Tutorial Example 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

 JavaScript Support in Web Browsers

 Including JavaScript Codes with HTML "script" Tags

Including 'script' Tags in String Literials

 Escaping 'script' Tags in String Literials

 Using HTML Entities to Protect HTML Tags

 Including JavaScript Codes as External Files

 DOM API - The "document" Object

 DOM API - The "window" Object

 Event Listeners and Objects

 'javascript:' Pseudo-URL Addresses

 Server-Side and Client-Side Web Scripting

 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

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
Including 'script' Tags in String Literials