Placing Code

Placing Code


When you want to place the script somewhere in the HTML document, you need to choose where to put it. Technically, you may place it anywhere between the <HTML> and </HTML> tags that enclose the whole document. Actually, the two possibilityes are the <HEAD></HEAD> portion and the <BODY></BODY> portion. Because the <HEAD></HEAD> portion is evaluated first, some developers choose to place their JavaScript here. A single HTML document may contain any number of scripts. You can place some of the scripts in the <HEAD></HEAD> portion and others in the <BODY></BODY> portion of the page.

The following code demonstrates this:

Scripts in the Head.

<html>
<head>
<title> Javascript Document</title>
<script type=”text/javascript”>
//script statement(s) here
...
</script>
</head>
<body>
</body>
</html>

A Script in the Body

<html>
<head>
<title> Javascript Document</title>
</head>
<body>
<script type=”text/javascript”>
//script statement(s) here
...
</script>
</body>
</html>

Scripts in the Head and Body

<html>
<head>
<title>Javascript Document</title>
<script type=”text/javascript”>
//script statement(s) here
...
</script>
</head>
<body>
<script type=”text/javascript”>
//script statement(s) here
...
</script>
</body>
</html>

Placing JavaScript code in another file

<html>
<head>
<title> Javascript Document</title>
</head>
<script type=”text/javascript” src=”scripts/javafile.js”></script>
<body>
</body>
</html>