Search notes:

HTML element: script

The <script> tag is intended to include script and data blocks into a HTML documnet, not to store content which is (directly) rendered for the user.

Attributes

Attributes that are relevant to the <script> element are
src Address (URL?) of the script
type empty, or module: a JavaScript module, or importmap: an import map. Any other value interprets the script as an (unprocessed) data block.
nomodule A boolean attribute that allows to prevent a script from being executed in user agents that support module scripts.
async A boolean attribute to execute the script as soon as it is available
defer A boolean attribute to defer execution
crossorigin A CORS setting attribute.
integrity
referrerpolicy

src

In order to load an external JavaScript resource, the src attribute must be set to the URL where that resource is located.

type

The type attribute specifies the kind of the script.
In order to indicate that the script is a classic script, this attribute can be omitted, set to the empty string or a JavaScript MIME type essence match such as text/javascript.
text/javascript: default
application/ld+json: See JSON-LD
text/ecmascript
application/ecmascript
application/javascript

Module type

Setting type="module" specifies the script to be a JavaScript module script.
See also the Javascript import declaration

defer, async

defer and async are boolean attributes.
Classic scripts:
  • async causes the script to be fetched in parallel and to be parsed and executed as soon as possible (sometimes even before parsing is complete)
  • defer causes the script to be fetched in parallel and to be evaluated when is finished parsing.
  • These attributes cannot be specified for class scripts if the src attribute is present.
  • Because historical browsers that only support defer, but not async, both attributes can be specified. In this case, it behaves as if only defer was specified.
Module scripts:
  • defer has no effect
  • ???

See also

Using the <script> tag to define inline or external JavaScript code.
Using the <script> tag to specify the source code for WebGL.
There is also a <noscript> tag.
An example to examine when an (inline) Javascript is executed
HTML elements
document.scripts
document.currentScript returns the currently running <script> tag (which is not a JavaScript module).

Links

The <script> element in the living standard.

Index