Where should we use script tag in the HTML ?
Last Updated :
05 Aug, 2024
In this article, we are going to learn about that what are the reasons that we should not add the <script> tag at the top of the body tag inside the HTML code instead of we should add the script file at the end of the HTML code.
Script Tag: It is used to add JavaScript inside the HTML code and to make the behavior of the website dynamic.
There are two approaches for adding the script file in the HTML that are:
1. TOP Approach: In the top approach we will add the script file either in the head tag or at the top of the body tag.
Syntax:
- First Approach in the head tag
<html>
<head>
<script src="path/filename.js"></script>
</head>
<body>
...
</body>
</html>
- Second Approach at the top of the body tag
<html>
<head>
</head>
<body>
<script>
// Your Javascript code here
</script>
...
</body>
</html>
Example: So, we will be understanding with the help of an implementation using the second approach as explained below:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible"
content="IE=edge" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0" />
<title>Script Tag</title>
<style>
* {
box-sizing: border-box;
margin: 0;
}
#btn {
margin: 50px;
width: 100px;
height: 40px;
font-size: medium;
color: red;
border-radius: 10px;
cursor: pointer;
background-color: antiquewhite;
}
#btn:hover {
background-color: white;
color: black;
}
</style>
</head>
<body>
<script>
const button = document.getElementById("btn");
button.addEventListener("click", function (e) {
alert("The button is Pressed");
});
</script>
<div>
<button id="btn">Click Here</button>
</div>
</body>
</html>
Output:
Explanation: We have used the JavaScript code over here at the top of the body tag and added an event Listener 'Click' and that should display the text as an alert message on clicking the button but it doesn't show because of the loading of script file before loading the HTML code.
Disadvantage of the TOP Approach:
- As we can see that we are getting no alert if we are clicking the button because after the body tag the script tag has loaded and inside the script tag there is an event listener that has been applied on the button that has not been created till now as we can see in the below DOM tree for more explanation.
DOM Tree For above HTML CodeSo that's why we should not add the script tag at the top of the body tag as you can see above. It is a major disadvantage of adding the script file at the top of the HTML.
2. END Approach: In the end approach we will be using the script tag at the bottom of the body tag:
Syntax:
<html>
<head>
...
</head>
<body>
...
<script>
// Your javascript code here
</script>
</body>
</html>
Example: Let's understand the End Approach with the help of an implementation as explained below:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible"
content="IE=edge" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0" />
<title>Script Tag</title>
<style>
* {
box-sizing: border-box;
margin: 0;
}
#btn {
margin: 50px;
width: 200px;
height: 60px;
font-size: larger;
color: red;
border-radius: 10px;
cursor: pointer;
background-color: antiquewhite;
}
#btn:hover {
background-color: white;
color: black;
}
</style>
</head>
<body>
<div>
<button id="btn">Click Here</button>
</div>
<script>
const button = document.getElementById("btn");
button.addEventListener("click", function (e) {
alert("The button is Pressed");
});
</script>
</body>
</html>
Output:
Explanation: In this, we have added the same javascript as in the above code that was added an event Listener on the button, and here we will be seeing that the 'onClick' event is working on the button because of the loading of HTML code first and then the addition of javascript file second due to which all the HTML element are found by the query Selectors of DOM API.
Advantages of the END Approach:
- By using the End Approach you will not get any error in the JS code on the console of the browser and all the element tags are found by the DOM API that is present in the HTML code.
Similar Reads
Why is HTML used in Web Pages ?
Web pages are built using the HyperText Markup Language or HTML. It is a markup language that defines a webpage's content and structure using tags. These tags provide instructions to web browsers on how to show links, photos, videos, text, and other content. Consider HTML as a house's plan, detailin
9 min read
When to use <samp> tag in HTML ?
The <samp> tag in HTML is used to define sample output from a computer program. It is typically used to represent text that is the output of a program or script. The <samp> tag is an inline tag and its content is rendered on the browsers in a monospaced font. Syntax: <samp> Sample
1 min read
What is the use of <canvas> Tag in HTML ?
HTML <canvas> Tag allows you to create graphics, animations, and interactive content directly on a web page using JavaScript. It provides a drawing surface where you can use various JavaScript functions to create dynamic and visually appealing content. Here are key aspects of the <canvas
1 min read
What is the use of the âno-jsâ class in HTML ?
Purpose: The primary purpose of the no-js class is to allow the use of CSS to style JavaScript-free pages, i.e. defining CSS styles for JavaScript-enabled browsers as well as for JavaScript-disabled browsers. Thus, the "no-js" class will only be present if JavaScript has been disabled. This enables
1 min read
What is the use of integrity attribute in HTML ?
In this article, we will learn the purpose of using the integrity attribute in HTML & will also understand its implementation through an example. HTML introduced a new attribute called "integrity". The purpose of the integrity attribute is used to give permission to the browser to check the fetc
2 min read
Which specific tag is used to represent the article in HTML ?
In this article, we will see how to represent the article in HTML. The <article> tag is one of the new sectioning element in HTML5. The HTML <article> tag is used to represent an article. More specifically, the content within the <article> tag is independent of the other content of
2 min read
Top 10 Uses of HTML in the Real World
HTML (Hyper Text Markup Language) - is the backbone of the web- a powerful yet simple language that forms the structure of nearly every website we use today. While it's often associated with basic web page creation, HTML's capabilities extend far beyond that. With the arrival of HTML 5, it has evolv
8 min read
Which specific tag is used to blink the text in HTML ?
The HTML <blink> tag is used to create a blinking text that flashes slowly. It has been obsolete all told fashionable browsers whereas some browsers never supported it in the least. This tag was also never standardized by hypertext mark-up language. You may recreate an identical visual impact
2 min read
What are physical tags in HTML?
Physical Tags are used to indicate that how specific characters are to be formatted or indicated using HTML tags. Any physical style tag may contain any item allowed in text, including conventional text, images, line breaks, etc. Although each physical tag has a defined style, you can override that
5 min read
When CDATA section is necessary within a script tag ?
A CDATA section within a script tag is used to include data within a script element that should not be parsed as JavaScript. CDATA stands for Character Data, and it allows you to include characters that might otherwise be interpreted as HTML or XML markup. Without a CDATA section, the browser might
3 min read