You can place <script> tag in HEAD element of the HTML document. The <script> in the head element executes when you perform some action.
Example: when you click a button.
Demo
Let's create a document called ScriptInHead.html
Syntax: The syntax for placing snippets in the HEAD section is.
- <HEAD>
- <SCRIPT type=”text/javascript”>
- Script goes here
- </SCRIPT>
- </HEAD>
Write the script in HEAD element
Execute the script by Opening the file in the Web Browser. If you are using Internet Explore click on
“Allow Blocked Content” to allow the script to execute and you are using Mozilla Firefox then click on allow
“ActiveX Controls”.
This example creates a webpage by adding the script in the HEAD element. The script displays a message when the page loads.