Including JavaScript Codes with HTML "script" Tags

This section provides a quick description of how to include JavaScript codes into HTML documents with 'script' tags. A tutorial example is provided on calling a function defined in the 'head' tag.

JavaScript code is designed to be executed mainly by Web browsers on client systems. So JavaScript code needs to be embedded in HTML documents to be delivered to browsers.

HTML document language standard supports the "script" tag that allows you to include JavaScript code in HTML documents. The syntax of the "script" tag looks like this:

<script type="text/javascript" ...>
   ... JavaScript statements ...
</script>

In a single HTML document, multiple "script" tags with JavaScript codes can be placed in different locations. The exact locations and relations of "script" tags are described in following simple rules:

Based these rules, we could define a user function in the "head" tag of the document, and call it later in a "pre" tag in the same document. Here is a simple tutorial example that shows this interesting behavior:

<html>
<!-- Hello_World_Head.html
   Copyright (c) 2013 by Dr. Herong Yang, herongyang.com
-->
<head>
<title>printHello() Function in the "head" Tag</title>
<script type="text/javascript">
function printHello() {
   document.write('Hello World!');
}
</script>
</head>
<body>
<pre>
<script type="text/javascript">
   printHello();
</script>
</pre>
</body>
</html>

Of course, the output of this sample JavaScript page will be a simple text on the page: "Hello World!".

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

 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

 DOM API - The "window.open" Method

 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