The <script> tag adds executable code, usually JavaScript, to a web page.
The code can be declared in-line, inside the <script> tag, or linked from an external source with the src attribute.
A <script> tag with JavaScript code. It displays an alert box when the page is loaded.
<script>
alert('Hello there!');
</script>
The <script> element embeds code, like JavaScript, in a page.
This tag and the code are invisible to the user.
The <script> tag can also load external script files with src.
A single page can have multiple <script> elements.
A <script> element with JavaScript that continuously increments the value of a progress bar.
This causes the control to continually fill.
<div>Loading files... </div>
<progress id="myprogress" value="0" max="100"></progress>
<script>
setInterval( function(){
let value = document.getElementById("myprogress").value;
value = Math.min(value + .1, 100) % 100;
document.getElementById("myprogress").value = value;
}, 10 );
</script>
This table lists the <script> tag attributes.
| Attribute | Value | Description |
|---|---|---|
| src | URL | The URL of an external script file. |
| type | media-type | Media type of the script. |
| charset | charset | Defines the character encoding of the external script. |
| async | async | Execute script asynchronously for loaded external scripts. |
| defer | defer | Executes script when browser has finished evaluating the code. |
| id | identifier | Defines a unique identifier for the script block. |
| crossorigin | anonymous use-credentials |
Defines how the element handles cross-origin requests. |
For additional global attributes see our global attributes list.
Do not use the attributes listed below. They are no longer valid on the script tag in HTML5.
| Attribute | Description | Alternative |
|---|---|---|
charset |
Defines the character encoding of the script. | n/a |
language |
Defines the language of the script. | type attribute |
A <script> tag can go in the <head> section, <body> section, or after the <body> section.
It all depends on when you want the script to load.
Keeping <script> elements in a <head> section keeps the main content area neat and uncluttered.
If code executes during page load, for example: document.write() to generate content, then the required <script> needs to be placed before the point where it is used.
Performance wise, the best place for <script> elements is just before the </body> closing tag.
Placing <script> after </body> will load all HTML elements first before executing any script.
The <script> tag is part of a group of tags that
define the structure and functionality of a web page.
This group is referred to as the Page tag group.
Together, they allow you to create solid, well-structured web pages.
Here is the complete list.
| Element | Description |
|---|---|
| <!DOCTYPE> | Must appear on the first line of a page. Specifies the HTML version |
| <html> | Defines the root container for an HTML document |
| <head> | Creates a head container that holds page-level metadata elements |
| <meta> | Provides metadata about a web page |
| <link> | Defines a link to an external source, such as a style sheet |
| <base> | Sets the base URL for all relative URLs on a page |
| <script> | Adds JavaScript to a page. Either client- or server-side |
| <style> | Adds CSS style elements to a page |
| <title> | Specifies the page title that displays in the browser's tab |
| <body> | Specifies a container for the content of the page, with text, links, images, etc. |
Here is when <script> support started for each browser:
![]() Chrome
|
1.0 | Sep 2008 |
![]() Firefox
|
1.0 | Sep 2002 |
![]() IE/Edge
|
1.0 | Aug 1995 |
![]() Opera
|
1.0 | Jan 2006 |
![]() Safari
|
1.0 | Jan 2003 |