Table of Contents
JavaScript Where To
The <script> Tag
- In HTML, JS codes are inserted between <script> and </script> tags.
JS Functions and Events
- A JavaScript function is a block of code, that can be executed when “called”.
JavaScript in <head> or <body>
- JS can be placed in the <body>, or in the <head> section of an HTML page, or in both.
- JavaScript in <head>
- JavaScript in <body>
- In this example, a JavaScript function is placed in the <body> section of an HTML page.
- The function is invoked (called) when a button is clicked.
Note :- Placing scripts at the bottom of the <body> element improves the display speed, because script interpretation slows down the display.
External JavaScript
- Scripts can also be placed in external files.
- You can place an external script reference in <head> or <body> as you like.
- The script will behave as if it was located exactly where the <script> tag is located.
- External scripts cannot contain <script> tags.
External JavaScript Advantages
- It separates HTML and Java code.
- It makes HTML and JavaScript easier to read and maintain.
- Cached JavaScript files can speed up page loads.