Where to Put Your Scripts

Scripts can be put in one of two places on an HTML page: between the <head> and </head> tags (called a header script), or between the <body> and </body> tags (a body script). Script 2.1 is an example of a body script.

There is an HTML container tag that denotes scripts, which, as you would guess, begins with <script> and ends with </script>.

To write your first script:

1.
<script type="text/javascript">
Here’s the opening script tag. This tells the browser to expect JavaScript instead of HTML. The type="text/javascript" attribute tells the browser that the script is plain text, organized as JavaScript.
2.
document.write("Hello, world!");
Here’s the first line of JavaScript: It takes the document window and writes “Hello, ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.