HTML5 <script> Tag


Write "Hello world" with JavaScript:

<script type="text/javascript">
document.write("Hello World!")

Try it yourself »

Definition and Usage

The <script> tag is used to define a client-side script, such as a 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.

Differences Between HTML 4.01 and HTML5

The type attribute is required in HTML 4, but optional in HTML5.

The "async" attribute is new in HTML5.

Some HTML 4.01 attributes are not supported in HTML5.

Tips and Notes

Note: There are several ways a script can be executed:

The async attribute is "true": The script will be executed asynchrously with the rest of the page, so the script will be executed while the page continues the parsing.

The async attribute is "false", but the defer attribute is "true": The script will be executed when the page is finished with the parsing.

Both the async attribute and the defer attribute is "false": The script will be executed immediately, and the page will wait for the script to finish before continuing the parsing.

Tip: If there is a src attribute, the <script> element must be empty.


New : New in HTML5.

Attribute Value Description
asyncNew async Defines if the script should be executed asynchronously or not
type text/javascript
and more...
Specifies the MIME type of the script. The default value is "text/javascript"
charset charset Defines the character encoding used in script
defer defer Indicates that the script is not going to generate any document content. The browser can continue parsing and drawing the page
src URL Defines a URL to a file that contains the script (instead of inserting the script into your HTML document, you can refer to a file that contains the script)
xml:space preserve Not supported in HTML5