Where use JavaScript

|

Where should I put JavaScript?
It depends. You have three choices. Since a web page can have multiple scripts, you can use them all on the same page! Scripts can be in the <head> section, the <body> section, or an external file.

Scripts in the <head> section are loaded first, before the rest of the web page is loaded. Scripts in the <body> section are loaded at their position on the web page.

Are scripts executed automatically?
Not all scripts are executed automatically. Scripts with functions are not executed until they are called. You will learn that a script can be called when it is triggered by an event. One such event is clicking a button.

What is an external script?
An external script is saved in a separate file, like an external style sheet. By convention, the filename with javaScript scripts ends with .js (for JavaScript, naturally).

How do I use an external script?
In the <script> tag, add the src attribute to identify the location of the JavaScript file; for example:

<script src="global.js"></script>




 

©2009