The HTML <script> tag
Definition and Usage
<script>
tags are used to embed client-side scripts.
<script>
The element either contains script statements or is src attribute Points to an external script file.
The most commonly used script type is JavaScript, although browsers do indeed support some other script languages.
Common uses of JavaScript include image processing, form validation, and dynamic content changes.
Note:The type of the script element depends on its usage. The script element located within the head element is a metadata element, and the script element located within other elements (such as body or sectionThe elements within parentheses are phrase elements.
See also:
HTML Tutorial:HTML Script
HTML DOM Reference Manual:Script Object
JavaScript Tutorial:Learning JavaScript
Example
Example 1: Define embedded script in the document
Write "Hello JavaScript!" using JavaScript:
<script> document.getElementById("demo").innerHTML = "Hello JavaScript!"; </script>
Example 2: Load an external script library
<script src="simple.js"></script>
Tips and comments
Tip:For browsers that disable scripts or do not support scripts, see also <noscript> element.
Tip:If you want to learn more about JavaScript, please visit our JavaScript Tutorial.
Attribute
Attribute | Value | Description |
---|---|---|
async | async | Specify that the script should be downloaded in parallel while parsing the page and executed immediately when available (before parsing is complete) (only for external scripts). |
crossorigin |
|
Set the request mode to HTTP CORS request. |
defer | defer | Specify that the script should be downloaded in parallel while parsing the page and executed after the page is fully parsed (only for external scripts). |
integrity | filehash | Allow the browser to check the fetched script to ensure that the code will never be loaded if the source code is tampered with. |
nomodule |
|
Specify that the script should not be executed in browsers that support ES2015 modules. |
referrerpolicy |
|
Specify which referrer information to send when fetching the script. |
src | URL | Specify the URL of the external script file. |
type | Script type | Specify the media type of the script. |
Differences between HTML and XHTML
In XHTML, the content within scripts is declared as #PCDATA (instead of CDATA), which means entities will be parsed.
This means that in XHTML, all special characters should be encoded, or all content should be wrapped in a CDATA section:
<script type="text/javascript"> //<![CDATA[ var i = 10; if (i < 5) { // some code {} //]]> </script>
Global Attributes
<script>
The tag also supports Global Attributes in HTML.
Default CSS Settings
Most browsers will display with the following default values <script>
Element:
script { display: none; {}
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |