HTML <script> tag

Updated: 09/12/2023 by Computer Hope
HTML script tag

When writing in HTML (hypertext markup language), the <script> tag is an inline element used to designate a client-side script on a web page. The <script> tag either points to an external script file or contains scripting statements. It is useful for adding features such as dynamic content or form validation. The following sections contain information about this tag, including an example of it in use, and related attributes and browser compatibility.

Example of <script> code

<script>document.write("Your browser supports JavaScript.")</script>
<noscript>Sorry, your browser does not support JavaScript.</noscript>

Example result

Example explanation

If you browser supports JavaScript, you see the message "Your browser supports JavaScript." If not, you see the message "Sorry, your browser does not support JavaScript."

Attributes

All HTML tags support standard attributes that define the settings of an HTML element. In addition to the standard settings, the <script> tag has the following unique attributes.

Note

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

Attribute Description
async Designates that the script should be executed asynchronously (external scripts only).
charset Designates the character encoding used with an external script file.
defer Designates that the script is executed once the page has finished parsing (external scripts only).
src Designates the URL (uniform resource locator)) of an external script file.
type Designates the script's media type.

Deprecated attributes

In addition to the above attributes, the <script> tag had the following deprecated attributes.

Attribute Description
xml:space Designates whether or not whitespace in code should be preserved.

Compatibility

Edge Internet Explorer Firefox Safari Opera Chrome
All versions All versions All versions All version All versions All versions

Author, Browser, Client-side, Compatibility, Container tag, CSS, HTML noscript tag, Parse, Web design terms, Whitespace