The <script> HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code. The <script> element can also be used with other languages, such as WebGL's GLSL shader programming language and JSON.
Definition and Usage
The <script> tag is used to embed a client-side script (JavaScript).
The <script> element either contains scripting statements, or it points to an external script file through the src attribute.
Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.
Tips and Notes
Tip: Also look at the <noscript> element for users that have disabled scripts in their browser, or have a browser that doesn't support client-side scripting.
Tip: If you want to learn more about JavaScript, visit our JavaScript Tutorial.
Browser Support
Attributes
Attribute
Value
Description
async
async
Specifies that the script is downloaded in parallel to parsing the page, and executed as soon as it is available (before parsing completes) (only for external scripts)
crossorigin
anonymous
use-credentials
Sets the mode of the request to an HTTP CORS Request
defer
defer
Specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing (only for external scripts)
integrity
filehash
Allows a browser to check the fetched script to ensure that the code is never loaded if the source has been manipulated
nomodule
True
False
Specifies that the script should not be executed in browsers supporting ES2015 modules
In XHTML, the content inside scripts is declared as #PCDATA (instead of CDATA), which means that entities will be parsed.
This means that in XHTML, all special characters should be encoded, or all content should be wrapped inside a CDATA section.
index.html
Example:HTML
<script type="text/javascript">
//<![CDATA[
let i = 10;
if (i < 5) {
// some code
}
//]]>
</script>
What is HTML <script> async Attribute
Definition and Usage
The async attribute is a boolean attribute.
If the async attribute is set, the script is downloaded in parallel to parsing the page, and executed as soon as it is available. The parsing of the page is interrupted once the script is downloaded completely, and then the script is executed, before the parsing of the rest of the page continues.
Note: The async attribute is only for external scripts (and should only be used if the src attribute is present).
Note: There are several ways an external script can be executed:
If async is present: The script is downloaded in parallel to parsing the page, and executed as soon as it is available (before parsing completes)
If defer is present (and not async): The script is downloaded in parallel to parsing the page, and executed after the page has finished parsing
If neither async or defer is present: The script is downloaded and executed immediately, blocking parsing until the script is completed
Browser Support
The numbers in the table specify the first browser version that fully supports the attribute.
Syntax
<script async>
How to add HTML <script> async Attribute
It is a script that will be downloaded in parallel to parsing the page, and executed as soon as it is available.
The crossorigin attribute sets the mode of the request to an HTTP CORS Request.
Web pages often make requests to load resources on other servers. Here is where CORS comes in.
A cross-origin request is a request for a resource (e.g. style sheets, iframes, images, fonts, or scripts) from another domain.
CORS is used to manage cross-origin requests.
CORS stands for Cross-Origin Resource Sharing, and is a mechanism that allows resources on a web page to be requested from another domain outside their own domain. It defines a way of how a browser and server can interact to determine whether it is safe to allow the cross-origin request. CORS allows servers to specify who can access the assets on the server, among many other things.
Tip: The opposite of cross-origin requests is same-origin requests. This means that a web page can only interact with other documents that are also on the same server. This policy enforces that documents that interact with each other must have the same origin (domain).
Tip: Also look at the integrity attribute.
Browser Support
The numbers in the table specify the first browser version that fully supports the attribute.
Syntax
<script crossorigin="anonymous|use-credentials">
Attribute Values
Value
Description
anonymous
use-credentials
Specifies the mode of the CORS request:
anonymous - A cross-origin request is performed. No credentials are sent
use-credentials - A cross-origin request is performed. Credentials are sent (e.g. a cookie, a certificate, a HTTP Basic authentication)
How to add HTML <script> crossorigin Attribute
Here is a link to a .js file on another server. Here we use both the integrity and crossorigin attributes.
If the defer attribute is set, it specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing.
Note: The defer attribute is only for external scripts (should only be used if the src attribute is present).
Note: There are several ways an external script can be executed:
If async is present: The script is downloaded in parallel to parsing the page, and executed as soon as it is available (before parsing completes)
If defer is present (and not async): The script is downloaded in parallel to parsing the page, and executed after the page has finished parsing
If neither async or defer is present: The script is downloaded and executed immediately, blocking parsing until the script is completed
Browser Support
The numbers in the table specify the first browser version that fully supports the attribute.
Syntax
<script defer>
How to add HTML <script> defer Attribute
It is a script that will be downloaded in parallel to parsing the page, and executed after the page has finished parsing.
index.html
Example:HTML
<!DOCTYPE html>
<html>
<body>
<h1>The script defer attribute</h1>
<script src="https://www.w3schools.com/tags/demo_defer.js" defer></script>
<p>The script above requests information from the paragraph below. Normally, this is not possible, because the script is executed before the paragraph exists.</p>
<p id="p1">Hello World!</p>
<p>However, the defer attribute specifies that the script should be executed later. This way the script can request information from the paragraph.</p>
</body>
</html>
Output should be:
What is HTML <script> integrity Attribute
Definition and Usage
The integrity attribute allows a browser to check the fetched script to ensure that the code is never loaded if the source has been manipulated.
Subresource Integrity (SRI) is a W3C specification that allows web developers to ensure that resources hosted on third-party servers have not been altered. Use of SRI is recommended!
When using SRI, the webpage holds the hash and the server holds the file (the .js file in this case). The browser downloads the file, then checks it, to make sure that it is a match with the hash in the integrity attribute. If it matches, the file is used, and if not, the file is blocked.
You can use an online SRI hash generator to generate integrity hashes: SRI Hash Generator
Browser Support
The numbers in the table specify the first browser version that fully supports the attribute.
Syntax
<script integrity="filehash">
Attribute Values
Value
Description
filehash
The file hashing value of the external script file
How to create HTML <script> integrity Attribute
It links to a CDN, using both the integrity and crossorigin atributes.
The noModule property of the HTMLScriptElement interface is a boolean value that indicates whether the script should be executed in browsers that support ES modules. Practically, this can be used to serve fallback scripts to older browsers that do not support JavaScript modules.
It reflects the nomodule attribute of the <script> element.
nomodule
True
False
Specifies that the script should not be executed in browsers supporting ES2015 modules
How to create nomodule on HTML <script> Tag
A boolean, true means that the script should not be executed in browsers that support ES modules, false otherwise.
index.html
Example:HTML
<script id="el" nomodule>
// If the browser supports JavaScript modules, the following script will not be executed.
console.log("The browser does not support JavaScript modules");
</script>
<script>
const el = document.getElementById("el");
console.log(el.noModule); // Output: true
</script>
What is HTML <script> referrerpolicy Attribute
Definition and Usage
The referrerpolicy attribute specifies which referrer information to send when fetching a script.
Browser Support
The numbers in the table specify the first browser version that fully supports the attribute.
Default. Sends the origin, path, and query string if the protocol security level stays the same or is higher (HTTP to HTTP, HTTPS to HTTPS, HTTP to HTTPS is ok). Sends nothing to less secure level (HTTPS to HTTP is not ok)
origin
Sends the origin (scheme, host, and port) of the document
origin-when-cross-origin
Sends the origin of the document for cross-origin request. Sends the origin, path, and query string for same-origin request
same-origin
Sends a referrer for same-origin request. Sends no referrer for cross-origin request
strict-origin-when-cross-origin
Sends the origin if the protocol security level stays the same or is higher (HTTP to HTTP, HTTPS to HTTPS, and HTTP to HTTPS is ok). Sends nothing to less secure level (HTTPS to HTTP)
unsafe-url
Sends the origin, path, and query string (regardless of security). Use this value carefully!
How to create HTML <script> referrerpolicy Attribute
How to add no-referrer-when-downgrade value on HTML <script> referrerpolicy Attribute
The URL is sent as a referrer when the protocol security level stays the same (e.g.HTTP→HTTP, HTTPS→HTTPS), but isn't sent to a less secure destination (e.g. HTTPS→HTTP).
no-referrer-when-downgrade
Default. Sends the origin, path, and query string if the protocol security level stays the same or is higher (HTTP to HTTP, HTTPS to HTTPS, HTTP to HTTPS is ok). Sends nothing to less secure level (HTTPS to HTTP is not ok)
How to add origin value on HTML <script> referrerpolicy Attribute
Only send the origin of the document as the referrer in all cases. The document https://example.com/page.html will send the referrer https://example.com/.
origin
Sends the origin (scheme, host, and port) of the document
How to add strict-origin value on HTML <script> referrerpolicy Attribute
Only send the origin of the document as the referrer when the protocol security level stays the same (e.g. HTTPS→HTTPS), but don't send it to a less secure destination (e.g. HTTPS→HTTP).
How to add strict-origin-when-cross-origin value on HTML <script> referrerpolicy Attribute
This is the user agent's default behavior if no policy is specified. Send a full URL when performing a same-origin request, only send the origin when the protocol security level stays the same (e.g. HTTPS→HTTPS), and send no header to a less secure destination (e.g. HTTPS→HTTP).
strict-origin-when-cross-origin
Sends the origin if the protocol security level stays the same or is higher (HTTP to HTTP, HTTPS to HTTPS, and HTTP to HTTPS is ok). Sends nothing to less secure level (HTTPS to HTTP)
How to add unsafe-url value on HTML <script> referrerpolicy Attribute
Send a full URL when performing a same-origin or cross-origin request. This policy will leak origins and paths from TLS-protected resources to insecure origins. Carefully consider the impact of this setting.
unsafe-url
Sends the origin, path, and query string (regardless of security). Use this value carefully!
The <script> HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code. The <script> element can also be used with other languages, such as WebGL's GLSL shader programming language and JSON.
Definition and Usage
The src attribute specifies the URL of an external script file.
If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again. Save the script file with a .js extension, and then refer to it using the src attribute in the <script> tag.
Note: The external script file cannot contain the <script> tag.
Note: Point to the external script file exactly where you would have written the script.
Browser Support
Syntax
<script src="URL">
Attribute Values
Value
Description
URL
The URL of the external script file.
Possible values:
An absolute URL - points to another web site (like src="http://www.example.com/example.js")
A relative URL - points to a file within a web site (like src="/scripts/example.js")