Tag Description
The script tag is used to define a client-side script, such as a JavaScript.
This tag has the form:
<script src="URL" type="MIME_type">
</script>
Note: The <script>
tag must be placed within the head element.
HTML5 and HTML4.01 Differences
The "type" attribute is required in HTML 4, but optional in HTML5.
The "async" attribute i new in HTML5.
The "xml:space" attribute is not supported in HTML5.
HTML5 | = New for HTML5 |
Attributes
Attribute | Value | Description |
---|---|---|
asyncHTML5 | async | Specifies that the script is executed asynchronously (only for external scripts) |
charset | charset | Specifies the character encoding used in an external script file |
defer | defer | Specifies that the execution of a script should be deferred (delayed) until after the page has been loaded |
src | URL | Specifies the URL of an external script file |
type | media_type | Specifies the media type of the script |
xml:space | preserve | Specifies whether whitespace in code should be preserved
HTML4:
![]() HTML5:
![]() |
Global Attributes
This tag supports the Global HTML Attributes.
Examples
The following example shows the basic use of this tag:
<script type="text/javascript">
document.write("Hello World!")
</script>
In HTML5, this can be written as:
<script>
document.write("Hello World!")
</script>
Browser Support
Chrome | Firefox | IE | Safari | Opera |
---|---|---|---|---|
Doctype Declaration Support
The following table lists the doctype declarations this element may appear in:
HTML4.01 / XHTML1.0 | XHTML 1.1 | HTML 5 | ||
---|---|---|---|---|
Traditional | Strict | Frameset | ||
Miscellaneous Information
Defined In: | HTML4 |
---|---|
Empty Tag: | No |