The JavaScript Diaries: Part 1 - Page 3
The JavaScript Diaries: Part 1
Where Does the Script Go?
There are three places where a script can be located:
|
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 ... //--> </script>
The opening tag, <script type="text/javascript"> , tells the browser:
|
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:
|
</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.
abstract | boolean | break | byte |
case | catch | char | class |
const | continue | debugger | default |
delete | do | double | else |
enum | export | extends | false |
final | finally | float | for |
function | goto | if | implements |
import | in | instanceof | int |
interface | long | native | new |
null | package | private | protected |
public | return | short | static |
super | switch | synchronized | this |
throw | throws | transient | true |
try | typeof | var | void |
volatile | while | with |
Wrap-Up
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- JavaScript is:
- an interpreted language
- a compiled language
- JavaScript is:
- subjective
- object based
- objective
- To comment out a line:
- Precede it with an asterisk, i.e. "*"
- Precede it with an asterisk and a forward slash, i.e. "*/"
- Precede it with two forward slashes, i.e. "//"
- JavaScript can only run on Windows.
- Semicolons are optional at the end of a statement.
- JavaScripts should not be formatted.
- It is best to place the JavaScript on each separate page.
- What is JavaScript, JScript, and EMCAScript?
- What is the purpose of the
<script type="text/javascript">
tag? Explain how it works. - What is the purpose of the
<script type="text/javascript" src="/javascript/scriptfile.js">
tag? Explain how it works. - What extension should be used for an external JavaScript file?
- What are "reserved words" used for?
Continue on to "The JavaScript Diaries: Part 2"
Created: April 15, 2005
URL: https://webreference.com/programming/javascript/diaries/1