Horje
What is HTML <script> Tag

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
referrerpolicy no-referrer
no-referrer-when-downgrade
origin
origin-when-cross-origin
same-origin
strict-origin
strict-origin-when-cross-origin
unsafe-url
Specifies which referrer information to send when fetching a script
src URL Specifies the URL of an external script file
type scripttype Specifies the media type of the script

How to create HTML <script> Tag

It writes "Hello JavaScript!" with JavaScript.
index.html
Example: HTML
<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script> 

Output should be:

How to create HTML <script> Tag

How to set Default CSS Settings on HTML <script> Tag

Most browsers will display the <script> element with the following default values.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<style>
script {
  display: none;
}
</style>
<body>

<h1>The script element</h1>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script> 

</body>
</html>

Output should be:

How to set Default CSS Settings on HTML <script> Tag

What is Differences Between HTML and XHTML

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:


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.
index.html
Example: HTML
<p id="p1">Hello World!</p>
<script src="demo_async.js" async></script>

Output should be:

How to add HTML <script> async Attribute

What is HTML <script> crossorigin Attribute

Definition and Usage

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.

<script
            type="text/javascript"
            src="my_script.js"
            crossorigin="anonymous"
        ></script>

Output should be:

How to add HTML <script> crossorigin Attribute

How to add HTML <script> crossorigin anonymous Attribute

Here is a link to a .js file on another server. Here we use both the integrity and crossorigin attributes.

index.html
Example: HTML
        <script
            type="text/javascript"
            src="my_script.js"
            crossorigin="anonymous"
        ></script>

Output should be:

How to add HTML <script> crossorigin anonymous Attribute

How to add HTML <script> crossorigin use-credentials Attribute

Here is a link to a .js file on another server. Here we use both the integrity and crossorigin attributes.

index.html
Example: HTML
 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="use-credentials">
</script> 

What is HTML <script> defer Attribute

Definition and Usage

The defer attribute is a boolean attribute.

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:


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:

How to add HTML <script> defer Attribute

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.
index.html
Example: HTML
 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous">
</script> 

What is nomodule HTML <script> Tag

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.

Syntax

<script referrerpolicy="no-referrer|no-referrer-when-downgrade|origin|origin-when-cross-origin|same-origin|strict-origin-when-cross-origin|unsafe-url">

Attribute Values

Value Description
no-referrer No referrer information is sent
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)
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

It sets the referrerpolicy for a script.

index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="origin"></script>

How to add no-referrer value on HTML <script> referrerpolicy Attribute

The Referer header will be omitted entirely. No referrer information is sent along with requests.

index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="no-referrer"></script>

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)
index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="no-referrer-when-downgrade"></script>

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
index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="origin"></script>

How to add origin-when-cross-origin value on HTML <script> referrerpolicy Attribute

Send a full URL when performing a same-origin request, but only send the origin of the document for other cases.

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
index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="origin-when-cross-origin"></script>

How to add same-origin value on HTML <script> referrerpolicy Attribute

A referrer will be sent for same-site origins, but cross-origin requests will contain no referrer information.

same-origin Sends a referrer for same-origin request. Sends no referrer for cross-origin request
index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="same-origin"></script>

JS Example of same-origin value on HTML <script> referrerpolicy Attribute

JS Example

script.js
Example: JS
const scriptElem = document.createElement("script");
scriptElem.src = "/";
scriptElem.referrerPolicy = "same-origin";
document.body.appendChild(scriptElem);

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).

index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="strict-origin"></script>

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)
index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="strict-origin-when-cross-origin"></script>

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!
index.html
Example: HTML
<script src="myscripts.js" referrerpolicy="unsafe-url"></script>

What is HTML <script> src Attribute

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")

How to add HTML <script> src Attribute

It points to an external JavaScript file.

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h1>The script src attribute</h1>

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

</body>
</html>

Output should be:

How to add HTML <script> src Attribute

What is HTML <script> type Attribute

Definition and Usage

The type attribute specifies the type of the script.

The type attribute identifies the content between the <script> and </script> tags.


Browser Support

Syntax

<script type="scripttype">

Attribute Values

Value Description
scripttype Specifies the type of the script.

Some common values:
  • A JavaScript MIME type like: text/javascript (default)module:
  • Another MIME type. src attribute will be ignored

Look at IANA Media Types for a complete list of standard media types.

How to create HTML <script> type Attribute

It is a script with the type attribute specified.
index.html
Example: HTML
<p id="demo"></p>

<script type="text/javascript">
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>

Output should be:

How to create HTML <script> type Attribute




html script

Related Articles
What is HTML <!--...--> Tag HTML Tag
What is HTML <!DOCTYPE> Declaration HTML Tag
What is HTML Elements and Doctypes HTML Tag
What is HTML <a> Tag HTML Tag
What is HTML <abbr> Tag HTML Tag
What is HTML <acronym> Tag HTML Tag
What is HTML <address> Tag HTML Tag
What is HTML <applet> Tag HTML Tag
What is HTML <area> Tag HTML Tag
What is HTML <article> Tag HTML Tag
What is HTML <aside> Tag HTML Tag
What is HTML <audio> Tag HTML Tag
What is HTML <b> Tag HTML Tag
What is HTML <base> Tag HTML Tag
What is HTML <basefont> Tag HTML Tag
What is HTML <bdi> Tag HTML Tag
What is HTML <bdo> Tag HTML Tag
What is HTML <big> Tag HTML Tag
What is HTML <blockquote> Tag HTML Tag
What is HTML <body> Tag HTML Tag
What is HTML <br> Tag HTML Tag
What is HTML <button> Tag HTML Tag
What is HTML <canvas> Tag HTML Tag
What is HTML <caption> Tag HTML Tag
What is HTML <center> Tag HTML Tag
What is HTML <cite> Tag HTML Tag
What is HTML <code> Tag HTML Tag
What is HTML <col> Tag HTML Tag
How to create HTML <colgroup> Tag HTML Tag
What is HTML <data> Tag HTML Tag
What is HTML <datalist> Tag HTML Tag
What is HTML <dd> Tag HTML Tag
What is HTML <del> Tag HTML Tag
What is HTML <details> Tag HTML Tag
What is HTML <dfn> Tag HTML Tag
What is HTML <dialog> Tag HTML Tag
What is HTML <dir> Tag HTML Tag
What is HTML <div> Tag HTML Tag
What is HTML <dl> Tag HTML Tag
What is HTML <dt> Tag HTML Tag
What is HTML <em> Tag HTML Tag
What is HTML <embed> Tag HTML Tag
What is HTML <fieldset> Tag HTML Tag
What is HTML <figcaption> Tag HTML Tag
What is HTML <figure> Tag HTML Tag
What is HTML <font> Tag HTML Tag
What is HTML <footer> Tag HTML Tag
What is HTML <form> Tag HTML Tag
What is HTML <frame> Tag HTML Tag
What is HTML <frameset> Tag HTML Tag
What is HTML <h1> to <h6> Tags HTML Tag
What is HTML <head> Tag HTML Tag
What is HTML <header> Tag HTML Tag
What is HTML <hgroup> Tag HTML Tag
What is HTML <hr> Tag HTML Tag
What is HTML <html> Tag HTML Tag
What is HTML <i> Tag HTML Tag
What is HTML <iframe> Tag HTML Tag
What is HTML <img> Tag HTML Tag
What is HTML <input> Tag HTML Tag
What is HTML <ins> Tag HTML Tag
What is HTML <kbd> Tag HTML Tag
What is HTML <label> Tag HTML Tag
What is HTML <legend> Tag HTML Tag
What is HTML <li> Tag HTML Tag
What is HTML <link> Tag HTML Tag
What is HTML <main> Tag HTML Tag
What is HTML <map> Tag HTML Tag
What is HTML <mark> Tag HTML Tag
What is HTML <menu> Tag HTML Tag
What is HTML <meta> Tag HTML Tag
What is HTML <meter> Tag HTML Tag
What is HTML <nav> Tag HTML Tag
What is HTML <noframes> Tag HTML Tag
What is HTML <noscript> Tag HTML Tag
What is HTML <object> Tag HTML Tag
What is HTML <ol> Tag HTML Tag
What is HTML <optgroup> Tag HTML Tag
What is HTML <option> Tag HTML Tag
What is HTML <output> Tag HTML Tag
What is HTML <p> Tag HTML Tag
What is HTML <param> Tag HTML Tag
What is HTML <picture> Tag HTML Tag
What is HTML <pre> Tag HTML Tag
What is HTML <progress> Tag HTML Tag
What is HTML <q> Tag HTML Tag
What is HTML <rp> Tag HTML Tag
What is HTML <rt> Tag HTML Tag
What is HTML <ruby> Tag HTML Tag
What is HTML <s> Tag HTML Tag
What is HTML <samp> Tag HTML Tag
What is HTML <script> Tag HTML Tag
What is HTML <search> Tag HTML Tag
What is HTML <section> Tag HTML Tag
What is HTML <select> Tag HTML Tag
What is HTML <small> Tag HTML Tag
What is HTML <source> Tag HTML Tag
What is HTML <span> Tag HTML Tag
What is HTML <strike> Tag HTML Tag
What is HTML <strong> Tag HTML Tag
What is HTML <style> Tag HTML Tag
What is HTML <sub> Tag HTML Tag
What is HTML <summary> Tag HTML Tag
What is HTML <sup> Tag HTML Tag
What is HTML <svg> Tag HTML Tag
What is HTML <table> Tag HTML Tag
What is HTML <tbody> Tag HTML Tag
What is HTML <td> Tag HTML Tag
What is HTML <template> Tag HTML Tag
What is HTML <textarea> Tag HTML Tag
What is HTML <tfoot> Tag HTML Tag
What is HTML <th> Tag HTML Tag
What is HTML <thead> Tag HTML Tag
What is HTML <time> Tag HTML Tag
What is HTML <title> Tag HTML Tag
What is HTML <tr> Tag HTML Tag
What is HTML <track> Tag HTML Tag
What is HTML <tt> Tag HTML Tag
What is HTML <u> Tag HTML Tag
What is HTML <ul> Tag HTML Tag
What is HTML <var> Tag HTML Tag
What is HTML <video> Tag HTML Tag
How to add HTML <wbr> Tag HTML Tag

Single Articles
How to create HTML <script> TagHTML Tag
How to set Default CSS Settings on HTML <script> TagHTML Tag
What is Differences Between HTML and XHTMLHTML Tag
What is HTML <script> async AttributeHTML Tag
How to add HTML <script> async AttributeHTML Tag
What is HTML <script> crossorigin AttributeHTML Tag
How to add HTML <script> crossorigin AttributeHTML Tag
How to add HTML <script> crossorigin anonymous AttributeHTML Tag
How to add HTML <script> crossorigin use-credentials AttributeHTML Tag
What is HTML <script> defer AttributeHTML Tag
How to add HTML <script> defer AttributeHTML Tag
What is HTML <script> integrity AttributeHTML Tag
How to create HTML <script> integrity AttributeHTML Tag
What is nomodule HTML <script> TagHTML Tag
How to create nomodule on HTML <script> TagHTML Tag
What is HTML <script> referrerpolicy AttributeHTML Tag
How to create HTML <script> referrerpolicy AttributeHTML Tag
How to add no-referrer value on HTML <script> referrerpolicy AttributeHTML Tag
How to add no-referrer-when-downgrade value on HTML <script> referrerpolicy AttributeHTML Tag
How to add origin value on HTML <script> referrerpolicy AttributeHTML Tag
How to add origin-when-cross-origin value on HTML <script> referrerpolicy AttributeHTML Tag
How to add same-origin value on HTML <script> referrerpolicy AttributeHTML Tag
How to add strict-origin value on HTML <script> referrerpolicy AttributeHTML Tag
How to add strict-origin-when-cross-origin value on HTML <script> referrerpolicy AttributeHTML Tag
How to add unsafe-url value on HTML <script> referrerpolicy AttributeHTML Tag
What is HTML <script> src AttributeHTML Tag
How to add HTML <script> src AttributeHTML Tag
What is HTML <script> type AttributeHTML Tag
How to create HTML <script> type AttributeHTML Tag

Read Full:
HTML Tag
Type:
Develop
Category:
Web Tutorial
Sub Category:
HTML Tag
Uploaded:
4 months ago
Uploaded by:
Admin
Views:
176


Reffered: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

Share on: