Definition and Usage
The <script> tag is used to embed client-side scripts or data blocks in an HTML document. It is most commonly used to include JavaScript code to implement dynamic HTML content or styles. This tag does not represent content for the user.
What Is a "Client-Side Script"?
A client-side script refers to a script that runs in the web browser (commonly called the "client") of the user who loaded the web page. A representative example is JavaScript.
In addition to JavaScript, data formats such as JSON can also be used, and 3D graphics can be implemented in a browser using the WebGL API through JavaScript.
What Is a "Data Block"?
A data block refers to a structured data code block, with JSON being a representative example.
These data blocks are used to provide data that controls dynamic functionality in client-side scripts or to supply structured information to search engines and other systems.
The <script> tag is mainly used to include JavaScript,
but it can also be used in various ways, such as including data blocks like JSON or using the WebGL API through JavaScript.
Basic Examples
Including Script Code
The following is an example of including JavaScript code, which is the most common use of <script>.
<script>
// Write JavaScript code
alert("Includes JavaScript code.");
</script>
The following is an example of including code from an external JavaScript file in <script>.
<!-- Include code from an external JavaScript file in an HTML document -->
<script src="example.js"></script>
Including Data Blocks
The following is an example of including a data block in <script>.
JSON is a representative structured data format, and the following is an example of including a JSON data block.
<script type="application/json">
{
"username" : "my_username",
"password" : "my_password",
"validation-factors" : {
"validationFactors" : [
{
"name" : "remote_address",
"value" : "127.0.0.1"
}
]
}
}
</script>
Attributes
Let's take a look at the attributes used with the <script> tag.
The <script> tag supports all global attributes.
src Attribute
The src attribute specifies the URL of an external script file. It can be used instead of writing script code inside the <script> tag.
<script src="script.js"></script>
type Attribute
The type attribute specifies the type (MIME type) of the script. The value of this attribute is one of the following:
Attribute Not Set (Default), Empty String, or JavaScript MIME Type
Indicates that the script is JavaScript.
Before HTML5, the JavaScript MIME type was explicitly specified (for example, type="text/javascript"). However, in HTML5, when the script is JavaScript, the type attribute itself is unnecessary, so it is commonly omitted.
<script type="text/javascript"></script>
<script></script>
module
When this value is used, the script is treated as a JavaScript module.
<script type="module" src="main.mjs"></script>
importmap
Represents JavaScript Import Maps. Import Maps provide a mechanism for controlling how JavaScript modules are imported in web applications.
For detailed explanations and content, see MDN - <script type="importmap">.
<script type="importmap">
{
"imports": {
"square": "./module/shapes/square.js",
"circle": "https://example.com/shapes/circle.js"
}
}
</script>
Any Other Value
The embedded content is treated as a data block and is not executed as a script. To represent data, a valid MIME type other than a JavaScript MIME type must be used. In this case, all other attributes, including the src attribute, are ignored.
See the following example.
<script type="application/json">
{
"username" : "my_username",
"password" : "my_password",
"validation-factors" : {
"validationFactors" : [
{
"name" : "remote_address",
"value" : "127.0.0.1"
}
]
}
}
</script>
async Attribute
This attribute is a Boolean attribute that does not require a value. If the attribute is present, it is applied; if absent, it is not applied.
HTML documents process marked-up content sequentially. When JavaScript code is encountered inside a <script> tag, the browser temporarily pauses parsing the HTML document to analyze and execute the JavaScript code. Only after the JavaScript execution completes does the browser resume processing the remaining content.
This sequential processing method is called "synchronous" processing.
However, when the async attribute is specified, the browser continues processing the HTML document while downloading the external JavaScript file. Once the download is complete, the JavaScript code is executed.
This approach of processing the HTML document and downloading the script simultaneously is called "asynchronous" processing.
When a script is loaded asynchronously, processing of the HTML document is not interrupted, and there is no need to wait for the script to load. This generally helps improve page performance and enhance the user experience.
One important thing to note:
To use the async attribute, the <script> tag must include the src attribute. In other words, it only applies when including an external JavaScript file in the <script> tag. If you write JavaScript code directly inside the <script> tag, the async attribute has no effect.
<script src="script.js" async></script>
async attribute is not applied
<script async>
// Write JavaScript code
alert("Includes JavaScript code.");
</script>
defer Attribute
This attribute is a Boolean attribute that does not require a value. If the attribute is present, it is applied. If it is absent, it is not applied.
Like the async attribute, the defer attribute downloads the script asynchronously. However, it executes the script only after the HTML document has finished parsing. Therefore, when the defer attribute is used, the script is not executed until the HTML document has finished parsing. In contrast, the async attribute executes the script as soon as the download is complete.
Like the async attribute, the defer attribute also requires the src attribute to be present on the <script> tag. Otherwise, this attribute has no effect.
<script src="script.js" defer></script>
defer attribute has no effect
<script defer>
// Write JavaScript code
alert("Includes JavaScript code.");
</script>
In addition, type="module" behaves like the defer attribute by default, so specifying the defer attribute does not change its behavior.
defer attribute does not change its behavior
<script type="module" src="main.mjs" defer></script>
In addition to the attributes described in this article, the <script> tag also supports attributes such as crossorigin, integrity, nomodule, nonce, referrerpolicy, and fetchpriority.
Technical Summary
| Permitted parents |
|
|---|---|
| Permitted content |
|
Implicit ARIA role |
No corresponding role |
Permitted ARIA roles |
No role permitted |
Specifications
| Specification | |
|---|---|
<script>
|
HTML Standard #the-script-element |
Browser Compatibility
| Tag |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<script>
|
1 | 12 | 1 | 3 |
References
See Also
- JavaScript Tutorial
- HTML <head> Tag – The Document Metadata Element
- HTML <link> Tag – The External Resource Link Element
- HTML <style> Tag – The Style Information Element
- HTML <title> Tag – The Document Title Element
- HTML <meta> Tag – The Metadata Element
- HTML <template> Tag – The Content Template Element
- HTML <noscript> Tag – The Noscript Element