This tutorial focuses on:
- The <script> tag
- When browsers cannot execute scripts
- Executing a script
The <script> tag
The <script> tag is used to place scripts on a webpage. When using the <script> tag, you have to use its type attribute to specify the language the script is written in.
Where to place a script?
You can place a script in three different places:
As shown in the example above, loading scripts from an external file can be done in both the body and head sections. This is achieved by using the src attribute which calls the script by its location.
When browsers cannot execute scripts
There are older browsers still in use that do not recognize the <script> tag and consequently will not be able to execute scripts. In such a case, the content inside the <script> tag will be displayed on the page as regular text. To prevent this from happening, the content of a script can be placed within comment tags. In such a case, older browsers that do not recognize the <script> tag will ignore the script and the content inside the <script> tag will not be displayed on the page. Browsers that can execute scripts will ignore the comments and execute the script anyway.
NOTE: This method also works for browsers that do recognize the <script> tag, but their settings have the scripting language you are using disabled.
Executing a script
Once you create a script, how do you execute that script? A script can be executed either automatically (if it issues some simple statements like writing text as in the above examples), or it can be triggered by some action such as when a page finishes completely loading, a form gets submitted, the mouse is clicked, and more.