HTML <script> Tag

Description

The <script> HTML element is intended to describe scripts; it may contain a link to the program or its text in a certain language. Scripts can be located in an external file and link to any HTML document. This approach allows you to use the same common functions on many web pages and speeds up their loading, because the external file is cached on first load, and the script is called faster on subsequent calls.
<script> can be located in the header or body of an HTML document in unlimited quantities. In most cases, the location of the script does not affect the operation of the program. However, scripts that must be executed first are usually placed in the title of the document.

Properties

Permitted Parents
Any element that accepts metadata content, or any element that accepts phrasing content
Content
Text
Start/End Tags
Start tag: required, End tag: required

Example

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

Attributes

Attribute Definition
async Specifies that the script is executed asynchronously (only for external scripts)
charset Defines the character encoding that the script uses.
defer Declares that the script will not generate any content. Therefore, the browser/user agent can continue parsing and rendering the rest of the page.
src Specifies a URI/URL of an external script.
type Specifies the scripting language as a content-type (MIME type).

Global Attributes

The <script> tag also supports the Global Attributes in HTML5

Event Attributes

The <script> tag also supports the Event Attributes in HTML5

Browser Support

Desktop
Explorer Edge Chrome Firefox Opera Safari
YesYes11YesYes
Tablets / Mobile
Android Chrome Firefox Opera Safari Samsung
YesYes4YesYesYes