HTML script Tag: How to Add JavaScript to a Web Page

html script tag

The script tag adds JavaScript to a web page in HTML. This lets the page run logic or load content. The browser reads this tag and runs the script. You can write the code inside the tag or link to an external file.

Understand the HTML <script> Tag

The <script> tag runs code in an HTML document. It supports JavaScript and other formats.

Here is the syntax:

<script src="file.js"></script>

You place the tag in the <head> or <body> section.

You use this tag to include or run JavaScript. This controls behavior, updates content, or tracks events.

Here are the differences between inline and external scripts:

Use inline scripts to write code directly in the HTML file:

<script>
  alert("Hello");
</script>

This example creates an alert pop-up on the page when the browser loads the script.

Use external scripts to keep code separate:

<script src="main.js"></script>

This loads and runs JavaScript from the main.js file when the browser reaches this tag.

The tag runs these content types:

  • JavaScript
  • JSON (as data blocks)
  • GLSL (for WebGL shaders)

Browsers pause HTML parsing. They load and run scripts. This blocks the page.

Use Async and Defer with Script Tag in HTML

Use the Async:

Async loads scripts and does not block HTML parsing.

<script async src="app.js"></script>

This script loads and runs on its own. It does not wait for HTML parse or other scripts.

Use Defer:

Defer loads the script while the browser parses HTML.

<script defer src="app.js"></script>

This script runs after the HTML loads completely and preserves the script order.

  • Async: The script runs as soon as it loads. The order is not fixed.
  • Defer: The script runs after the browser parses the HTML, and it follows the document’s order.

Attributes of the Script Tag in HTML

The type sets the script format. Default is text/javascript.

<script type="text/javascript" src="main.js"></script>

This tells the browser the script is JavaScript. This is the default if no type is set.

The src: Points to an external script file.

<script src="/path/to/app.js"></script>

This loads the script from a file located at the path you provide.

The async: Runs the script when loaded.

<script async src="track.js"></script>

This loads the script and runs it as soon as it’s ready, not in order with others.

Defer: The script runs only after the browser finishes parsing the HTML.

<script defer src="main.js"></script>

This prevents the script from blocking HTML parsing and runs it after the browser builds the page.

The crossorigin: It sets CORS rules for external scripts.

<script src="https://cdn.site.com/lib.js" crossorigin="anonymous"></script>

This allows the browser to request the script with CORS rules based on the attribute value.

The nomodule: Skips the script if the browser supports modules.

<script nomodule src="legacy.js"></script>

This makes older browsers load the script, and modern ones ignore it if they support modules.` It skips the script if the browser supports modules.

Put the script in the <head> if it must load first, or place it before </body> to prevent render delays.

Script Loading Strategies

Use defer for scripts that need the DOM.

<script defer src="dom-ready.js"></script>

This loads the script while the browser parses and runs it once the DOM becomes ready.

Use async for analytics or ads.

<script async src="ads.js"></script>

This loads ads.js in parallel. It runs as soon as it’s ready and does not block rendering.

Place scripts near the end of <body> to avoid delays.

<body>
  ...
  <script src="main.js"></script>
</body>

This delays script load until the page content loads, so it reduces render blocking.

Do not trust user input in scripts. Escape all inputs. Use CSP headers to block inline scripts. Avoid innerHTML for dynamic content.

All major browsers support the <script> tag. Modern browsers also support type="module", defer, and async.

Examples

Load JavaScript From a File:

<script src="main.js"></script>

This script loads code from main.js and blocks page rendering until the file fully loads and runs.

Run Inline Script:

<script>
  console.log("Page Loaded");
</script>

This script runs at the point where it appears. It writes “Page Loaded” into the browser console.

Use Async for Non-blocking Load:

<script async src="analytics.js"></script>

This script loads in parallel and runs immediately after it becomes ready, without delay from the rest of the page.

Use Defer for DOM-dependent Script:

<script defer src="ui.js"></script>

This script loads while parsing the page. It waits for DOM build to finish before running.

Wrapping Up

In this article, you learned what the <script> tag does and how to control its behavior. You also saw how to load scripts in the right order.

Here is a quick recap:

  • Use <script> to run or load JavaScript.
  • Use src to link to external files.
  • Use async to load fast, unordered scripts.
  • Use defer for scripts that depend on the DOM.
  • You have to escape input to avoid XSS and apply secure headers.

FAQs

What does the <script> tag do in HTML?

The <script> tag tells the browser to load and run code. Most scripts use JavaScript.

Can I put <script> tags in the <head> and <body>?

Yes. Scripts in <head> block loading. Scripts at the end of <body> run after the content loads.

How does async differ from defer in <script> tags?

async runs the script when it loads. defer waits until DOM parsing ends. defer keeps order. async does not.

How do I pass data inside a <script> tag?

Use <script type="application/json">. Read it with JavaScript using
textContent
and
JSON.parse()
.

Similar Reads

HTML ins Tag: How to Mark Inserted Text in a Document

The <ins> tag in HTML shows text that the author added after the original content. This helps readers know what…

HTML Mark Tag: How to Highlight Text

The mark tag highlights text in HTML and shows that the text has relevance in the content. It adds a…

What is HTML for Beginners and How it Works for Beginners

HTML builds the base for every web page. It sets structure and meaning so the browser knows what to show.…

HTML Document Structure Guide for Beginners

A web page must follow a clear structure so browsers can read and show it correctly. This guide helps you…

How to Create HTML Nested Lists with Examples

In this article, you will learn how the browsers read nested lists in HTML and how to use them with…

HTML del Tag: How to Mark Deleted Text in Pages

The HTML del tag shows text that a user or author removed from a page. A browser shows this text…

HTML strong Tag: How to Give Text Strong Importance

The HTML <strong> tag shows strong importance. This tag gives semantic meaning to text. Screen readers and search engines recognize…

HTML a Tag: How to Add Links to Web Page with Examples

The HTML a tag allows you to create links that move users from one page or resource to another. Understand…

HTML details Tag: How to Toggle Hidden Content

The <details> tag hides content and shows it when needed. It helps organize long pages. The tag shows extra info…

HTML p Tag: How to Create a Paragraph in HTML

Paragraphs help group-related text. They build clear, readable pages. You use them to split ideas or topics. The HTML p…

Previous Article

HTML details Tag: How to Toggle Hidden Content

Next Article

HTML base Tag: How to Set a Base URL for Links and Resources

Write a Comment

Leave a Comment

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


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.