DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Javascript

Placement of JavaScript in HTML

Share

For including JavaScript code in a HTML document, there is always a flexibility to put in head or body section.

A JavaScript function is nothing but a block of JavaScript code which can be executed when required.

In HTML, we need to insert JavaScript code between the <script>…</script> tags.

Most of the time we include JavaScript in <head>…</head> section, <body>…</body> section and both.

<script language="javascript">
document.getElementById("DIVID").innerHTML = "Hello World!";
</script>

In Head Section:

When you need to run a script for an event like click event, then you have to place the script in <head>…</head> section.

<html>
<head>
<script type="text/javascript">
function clickFunc() {
alert("Hello World!");
}
</script>
</head>
<body>
<input id="btn" type="button" onClick="clickFunc();" value="Click" />
</body>
</html>

In Body Section:

Suppose, you need a script to run just after the page loads. For example, after page loads you need to put some content in the page section. In that case, put your script in the <body>…</body> section.

<html>
<head>
</head>
<body>
<p>Hi there!</p>
<p id="paraID"></p>
<script type="text/javascript">
document.getElementById("paraID").innerHTML = "This is another paragraph!";
</script>
</body>
</html>

Read Also: JavaScript onerror() method to handle error

In both Head and Body section:

Also, you can put your script in both section depends on your requirements.

<html>
<head>
<script type="text/javascript">
function clickFunc() {
alert("Hello World!");
}
</script>
</head>
<body>
<p>Hi there!</p>
<p id="paraID"></p>
<script type="text/javascript">
document.getElementById("paraID").innerHTML = "This is another paragraph!";
</script>
<input id="btn" type="button" onClick="clickFunc();" value="Click" />
</body>
</html>

External File:

You can add also external JavaScript your HTML files.
Because, sometimes you can feel like a situation that you need to use same JavaScript on multiple pages of a site.

<html>
<head>
<script type="text/javascript" src="myJS.js" ></script>
</head>
<body>
<p>Hi there!</p>
</body>
</html>

In that case, you need to write all your source code in a single file with the extension “.js” and include as mention above.

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Your Complete Guide to the 461 Visa: Building a Life in Australia with Your New Zealand Partner

People usually stumble onto the 461 visa when they realise there’s no simple way to…

17 hours ago

Why Roarbank Is Transforming Everyday Banking in India

In today’s digital era, people look for convenience, transparency, and genuine rewards from their financial…

21 hours ago

Quality with Quantity: Importing LED Lights Can Save You Big Bucks

If you are planning to start a lighting business and looking for a supplier of…

2 days ago

11 Reasons Why Avoiding Plagiarism is Necessary for SEO and content marketing

If you are a copywriter or a content marketer, you will second my statement that…

5 days ago

6 Common Injuries After a Motorcycle Accident

Motorcycle accidents often lead to severe injuries because riders have minimal protection compared to drivers…

1 week ago

How Automation And SEO Can Improve Customer Experience

No business owner starts his or her business with the hope of making losses. Everyone…

1 week ago