HTML <script> Element

The HTML <script> element is used to embed executable code in an HTML document or reference an external script file, most commonly JavaScript.

Syntax

<script>
  document.getElementById("message").textContent = "Hello, World!";
</script>

The <script> element requires both an opening and closing tag. Script code can be written directly between the tags or loaded from an external file using the src attribute.

Attributes

The <script> element supports the global HTML attributes and the following commonly used element-specific attributes:

Attribute Description
src Specifies the URL of an external script file.
type Identifies the type of script or data contained by the element. The value module is used for JavaScript modules.
async Allows an eligible external script to be downloaded without blocking HTML parsing and executed as soon as it is available.
defer Allows an eligible external classic script to download while the document is being parsed and delays execution until parsing is complete.
crossorigin Controls how cross-origin requests for the script are handled.
integrity Provides cryptographic information that can be used to verify that a fetched script has not been unexpectedly modified.
referrerpolicy Specifies which referrer information should be sent when an external script is requested.
nonce Provides a cryptographic nonce that can be used with a Content Security Policy to permit a particular script.

Example

This example uses an inline JavaScript inside the <script> element to change the text of a paragraph when a button is clicked.

Play in Editor

Usage Notes

The <script> element is most commonly used for JavaScript that adds behavior and interactivity to a webpage. Scripts can respond to user actions, modify page content, validate data, communicate with servers, and perform many other tasks.

JavaScript can be written directly inside a <script> element or placed in a separate file and referenced with the src attribute. External files are useful when the same script is shared by multiple pages or when keeping JavaScript separate makes a project easier to maintain.

For ordinary JavaScript, the type attribute does not need to be set to text/javascript. Use type="module" when the script should be treated as a JavaScript module.

The async and defer attributes can change when eligible external scripts are downloaded and executed. Choosing the appropriate loading method can prevent scripts from unnecessarily delaying the parsing and display of a webpage.

A <script> element that uses the src attribute should not also contain executable JavaScript between its opening and closing tags.

Accessibility

JavaScript should enhance a webpage without making essential content or functionality unnecessarily difficult to use with a keyboard, screen reader, or other assistive technology.

When scripts change page content dynamically, make sure important changes can be recognized by users who may not see the visual update. Interactive controls should also use appropriate HTML elements and remain operable with a keyboard rather than relying only on mouse actions.

Whenever practical, provide essential information and functionality in a way that remains understandable if a script fails to load or cannot run.

Browser Support

Baseline: Widely available indicates a feature has been supported by core browsers for at least 30 months. At this stage, the feature is considered stable and safe for most websites to use without needing to worry about compatibility issues or fallbacks, as it is supported by the vast majority of users' devices and browser versions.

For detailed browser compatibility information, see Can I Use: HTML <script> Element .

The <script> element is closely related to the <noscript> element for fallback content when scripting is unavailable and the <link> element for linking external resources such as stylesheets.

Specifications

HTML Living Standard: The <script> Element