The JavaScript Diaries: Part 1

By Lee Underwood

Where Does the Script Go?

There are three places where a script can be located:

  1. In the top section of the page ("head"), between the <head> and </head> tags;
  2. In the middle section of the page ("body"), between the <body> and </body> tags; or
  3. In a separate file.

Placing the Script on the Page

When the script is placed on the page, it is located between a set of container tags that look like:

<script type="text/javascript">
... The script is placed here ...

The opening tag, <script type="text/javascript">, tells the browser:
  • it has encountered a script;
  • it is in text format; and
  • it is to be interpreted as JavaScript.
Note: There are several different ways to write this, but the W3C recommends the method shown here so that's what we'll use).

The next line, <!--, is the opening comment tag. It's used to hide the code from older browsers. The actual script starts on the third line. After the script ends, the comment tag is closed, //--> and the </script> tag tells the browser that the code is finished. The <script type="text/javascript"> and </script> tags are known as "container" tags because they "contain" the script within them.

Placing the Script in an External Page

When the script is located in an external file, the opening and closing tags shown above are not included. Only the script itself is included in the file. The file should have an extension of ".js." The HTML page would then contain a link to the JavaScript file, which would look like this:

<script type="text/javascript" src="/javascript/scriptfile.js"></script>

The opening tag, <script type="text/javascript" src="/javascript/scriptfile.js">, tells the browser:
  • it has encountered a script;
  • it is in text format;
  • it is to be interpreted as JavaScript; and
  • the script is located in an external file located at the given URL.
The closing tag, </script>, tells the browser that the code is finished. Nothing should be written between the opening and closing tag.

The link is usually located in the head of the document. In some cases it may be necessary to locate it within the body. It will depend on the script.

If the script is small and used only once, it's usually best to put it in the actual document. Otherwise, it's preferable to place the script in an external file.

By placing the script in an external file, it can be used on other pages without having to duplicate the code. The browser will then cache the script and use it on the other pages, making them to load faster. Using an external file also makes it easier if changes need to be made to the code. You only have the one file to change instead of making changes to each individual page.

Reserved Words

There are 59 "reserved" words. These are words that are used to give instructions to the JavaScript interpreter. They cannot be used for anything else and are listed below. We'll be looking at them in more detail as we use them. For now, just remember not to use them as variables.



That should just about do it for this installment. So far we've:

  • gained a little insight into the history of JavaScript;
  • found out what we needed to write the scripts;
  • gained some insight into what JavaScript is and what it isn't;
  • learned how to format a script;
  • learned how to make comments in a script;
  • and found out where to place a script in relation to the Web page.

Next time we'll be looking at variables and writing our first script. Remember, if you have any questions, you can send them to me or visit our JavaScript forums.

Review Questions

Answers to these questions will be given in the next installment (these questions are for your benefit. I won't be collecting your answers).

Multiple Choice
  1. JavaScript is:
    1. an interpreted language
    2. a compiled language
  2. JavaScript is:
    1. subjective
    2. object based
    3. objective
  3. To comment out a line:
    1. Precede it with an asterisk, i.e. "*"
    2. Precede it with an asterisk and a forward slash, i.e. "*/"
    3. Precede it with two forward slashes, i.e. "//"
True or False
  1. JavaScript can only run on Windows.
  2. Semicolons are optional at the end of a statement.
  3. JavaScripts should not be formatted.
  4. It is best to place the JavaScript on each separate page.
  1. What is JavaScript, JScript, and EMCAScript?
  2. What is the purpose of the <script type="text/javascript"> tag? Explain how it works.
  3. What is the purpose of the <script type="text/javascript" src="/javascript/scriptfile.js"> tag? Explain how it works.
  4. What extension should be used for an external JavaScript file?
  5. What are "reserved words" used for?

This article originally appeared on WebReference.com.

Page 3 of 3

Previous Page
1 2 3

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date