HTML Tags

HTML <script> Tag

The <script> tag is used to embed or reference JavaScript code in an HTML document.
It allows you to create interactive features, handle events, manipulate the DOM, and perform dynamic operations on a webpage.

Syntax

html

<script>
  // JavaScript code here
</script>

<!-- OR referencing an external file -->
<script src="script.js"></script>

Attributes

AttributeDescription
srcSpecifies the URL of an external script file.
typeSpecifies the scripting language. Default is text/javascript.
asyncLoads the script asynchronously without blocking HTML parsing.
deferLoads the script after the HTML is fully parsed.
crossoriginHandles CORS requests for external scripts.
nomodulePrevents execution in browsers that support ES modules.

Example

html

<!DOCTYPE html>
<html>
<head>
  <title>Script Tag Example</title>
  <script>
    function showMessage() {
      alert("Hello! This is a message from JavaScript.");
    }
  </script>
</head>
<body>
  <h1>Click the button to see the message</h1>
  <button onclick="showMessage()">Click Me</button>
</body>
</html>

Output

Browser Output

html

There is no direct output of the <script> tag itself, but it can add interactivity or dynamically modify page content. Please use our TryIt Editor to see the output.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

The <script> tag itself is invisible, but it can execute code that displays alerts, modifies content, or responds to user actions.

Notes

  • The <script> tag can contain inline JavaScript or reference an external file.
  • async and defer help improve page load performance.
  • Scripts can be placed in <head> or at the end of <body> for faster rendering.
  • Always ensure scripts are properly secured to prevent XSS vulnerabilities.

Conclusion

The <script> tag is essential for adding dynamic behavior and interactivity to HTML pages.
Although it does not produce visual content directly, it powers most modern web functionality, making it a core element in web development.