HTML script - how to use <script> tag

The HTML <script> tag is used to define a JavaScript. The script tag can be used to define scripts on the page or refer to external scripts.

(Very) old browsers may not support scripts and <noscript> tag can be used rendered comments instead.

Attributes

The script tag supports the global attributes.

async
Defines the external script be executed asynchronously
charset
Defines the character encoding for the external file
defer
Defines that the (external) script be executed once the page has finished loading and parsing
src
Defines the URL to the external JavaScript file
type
Defines the media type of the script file

Examples:

<script> function add(p1, p2){ return p1 + p2; } </script> <script src="file.js" type="text/javascript"></script>

Related HTML tags:

  • <noscript> - used to define content if script is not supported