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
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
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
HTML <script> Tag The HTML <script> tag embeds client-side scripts or links to external JavaScript files, enabling dynamic content, form validation, and style manipulation. Attributes like async, defer, and src control script execution and loading, enhancing the interactivity and performance of web pages.Syntax
3 min read
How to use PHP in HTML ? In this article, we will use PHP in HTML. There are various methods to integrate PHP and HTML, some of them are discussed below. You can add PHP tags to your HTML Page. You simply need to enclose the PHP code with the PHP starts tag <?php and the PHP end tag ?>. The code wrapped between these
2 min read
Most Commonly Used Tags in HTML HTML tags are the fundamental building blocks of web development, providing the structure and organization necessary for creating web pages.They include tags for headings, paragraphs, links, images, and more.Commonly used tags like <html>, <head>, and <body> are essential for creat
4 min read
HTML <noscript> Tag The <noscript> tag in HTML is used to display the text for those browsers which does not support the script tag or the browsers disable the script by the user. This tag is used in both <head> and <body> tags.Note: This tag is used in those browsers only which does not support scrip
2 min read
How to Add JavaScript in WordPress Page and Post? WordPress doesnât allow you to directly add JavaScript code to your site, you can achieve this by using plugins or manual methods. JavaScript is a popular programming language that allows you to add interactive features to your website. Learning how to add JavaScript to your WordPress site can help
3 min read
HTML Tags - A to Z List HTML Tags are fundamental elements used to structure and format content on web pages. They provide instructions to web browsers on how to render text, images, links, and other media.HTML tags are enclosed in angle brackets < > and usually come in pairs: an opening tag and a closing tag. The cl
15+ min read