Home » Online Computer Education » Learn how to Placing JavaScripts in the Head Section of the HTML page.

Learn how to Placing JavaScripts in the Head Section of the HTML page.

JavaScripts also can be placed anywhere within the head section of your HTML pages. Unlike scripts embedded within the body section of HTML pages, scripts embedded in the head section are not necessarily automatically executed when the page loads. In some cases, they are executed only when called for execution by other statements within the HTML page. Most JavaScript programmers move all functions and most variables to the head section because this ensures that they will be defined before being referenced by scripts located in the body section of the page.
Variables are containers for storing information in computer memory. Functions are groups of JavaScript statements that you can call to perform a specific task. I’ll talk more about the benefits of using functions and variables tomorrow.
The following statements show an HTML page with a JavaScript embedded in the head section. This script will automatically execute when the HTML page is loaded.

<HTML>
<HEAD>
<TITLE>Script 1.3 – Sample HTML Page</TITLE>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
window.alert(“This JavaScript is located in the head
section”);
</SCRIPT>
</HEAD>
<BODY>
</BODY>
<HTML>

This script displays a message in a pop-up dialog. I’ll explain how the script did this tomorrow.

Learn how to Placing JavaScripts in the Head Section of the HTML page.

 

This next HTML page contains an embedded JavaScript that will not automatically execute when the HTML is loaded by the browser.

<HTML>
<HEAD>
<TITLE>Script 1.4 – Sample HTML Page</TITLE>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
function DisplayMsg() {
window.alert(“This JavaScript is located in the head
section”);
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
This pop-up dialog is not displayed this time because the JavaScript statement that displays it has been embedded within a function called DisplayMsg(). As a result, the JavaScript is executed only when called upon to execute from someplace else in the HTML page. For now, don’t concern yourself with worrying about what a function is. Just understand that they can be used to create JavaScripts in the HTML page’s head section that do not execute automatically.

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *