In HTML, JavaScript code must be inserted between <script>
and </script>
tags.
Old JavaScript examples may use a type attribute:
<script type="text/javascript">
. The type attribute is not required. JavaScript is the default scripting language in HTML.
You can place any number of scripts in an HTML document. Scripts can be placed in the , or in the section of an HTML page, or in both.
Placing scripts at the bottom of the element improves the display speed, because script compilation slows down the display.
Scripts can also be placed in external files. External scripts are practical when the same code is used in many different web pages.
To use an external script, put the name of the script file in the src (source) attribute of a <script>
tag.
You can place an external script reference in or as you like.
The script will behave as if it was located exactly where the <script>
tag is located.
External JavaScript Advantages
- It separates HTML and code
- It makes HTML and JavaScript easier to read and maintain
- Cached JavaScript files can speed up page loads
External scripts can be referenced with a full URL or with a path relative to the current web page.