JavaScript

Tuesday April 8, 2025
updated: April 10, 2002

 
Home
Getting Started
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Resources
Credits

 

 


Lesson 2: Where to Put JavaScript

Scripts in the head section

When we want the script to be executed when a user triggers an event, (i.e. by clickign a button) we write the script in the head. This ensures the script loads before anyone uses it. Scripts that conatin functions are written in the Head. You will learn more about how Functions work in lesson 7.

<html>
<head>
<script>
JavaScript statements
</script>
</head>

Scripts in the body section

When we want a script to execute while the page loads into the browser, we put the script in the body. When you place a script in the body section it generates the content of the page.
<html>
<head>
</head>
<body>
<script >
JavaScript statements
</script>
</body>

The important points from this lesson are:

  • You can place an unlimited number of scripts in your document
  • Scripts can be placed in the body section, the head section or both
  • Scripts in the body section will execute while the page loads
  • Scripts in the head section will be executed when called
back button
next button

© 2002 Tracy Johnson