HTML - (Client-side) Script (Element)


A client-side script is a program that is:

  • linked (server-side script)
  • or directly embedded in an HTML document (in-line script)

Scripts in HTML have “run-to-completion” semantics, meaning that the browser will generally run the script uninterrupted before doing anything else, such as firing further events or continuing to parse the document.


The script may be defined:

If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

Using external files generally produces faster pages because the JavaScript and CSS files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested.

GUI / Background


An interface script is a script that respond to clicks or other user interactions.


see HTML - Web Worker. Background script


The SCRIPT element places a script within a document. This element may appear any number of times in the HEAD or BODY of an HTML document.


  defer=false >
<!-- The below script tag is then illegal and will not work -->
<script />


  • ContentType is the mime that defines the script language.
  • URI can point to an external file
  • async=false or true to execute script in parallel (ie the user agent can continue parsing and rendering and run the script as soon as possible.
  • defer=false or true indicates no document creation (ie no “document.write” in javascript) - the user agent can continue parsing and rendering and executes the script after.
  • integrity is a hash digest to control that the script was not tempered during the transport
  • crossorigin is a cors attribute


  • With async, the browser downloads the script asynchronously while it continues to parse the HTML document. When the script finishes downloading, parsing is blocked while the script executes. Example: for analytics scripts that should run early.
  • With defer, the browser downloads the script asynchronously while it continues to parse the HTML document. The script doesn't run until the parsing is complete. Example: A video player

it's non-conforming to have executable script in a script element when the src attribute is present.

The fallback element is the noscript element.

With the template element, they are script-supporting elements. They are used to support scripts.


  • src
<script src="" type="application/javascript"></script>

Including a script tag cause the browser to stop the rendering process of the page. It will wait for the script to load, execute, and then continue. This is generally bad and why you should always include scripts at the bottom of the page.


The type attribute of the script element define the language such as

  • text/tcl
  • text/javascript (javascript)
  • text/vbscript

See language


To calculate the integrity, see HTML - Integrity attribute


The program executes on the client's machine when:

  • the document loads,
  • or at some other time such as when an event occurs.

Load / Fetch

The default fetch mode for scripts that appear within a SCRIPT element is to :

This is bad for performance. You should always:


Scripts are executed every time a specific event occurs.


A script can be run:

  • immediately (default)
  • or in parallel

The flow of execution stay the same: when an algorithm B says to return to another algorithm A, it implies that A called B. Upon returning to A, the implementation must continue from where it left off in calling B.


An operation that is to run immediately must interrupt the currently running task, run itself, and then resume the previously running task.


In parallel, the currently running task is not interrupted, ie the subsequent steps are run, one after another, at the same time as other logic in the specification (e.g., at the same time as the event loop).

To run a script in parallel, you can use one of the resource fetch instruction such as the async attribute of the script element.

In parallel - Doc ref



HTML's support for scripts is independent of the scripting language. Scripts are evaluated by script engines that must be known to a user agent (The browser generally).

As HTML does not rely on a specific scripting language, document authors must explicitly tell user agents the language of each script.

This may be done either through:

  • a default declaration
  • or a local declaration.


For all scripts in a document with the following META declaration in the HEAD:


<META http-equiv="Content-Script-Type" content="type">

where type defines the type of the scripting language

In the absence of a META declaration, the default can be set by a Content-Script-Type HTTP header.


The value of the type attribute for a SCRIPT element overrides the default scripting language for that element.

Example of document with TCL as default language and the call of a vbsscript

<TITLE>A document with SCRIPT</TITLE>
<META http-equiv="Content-Script-Type" content="text/tcl">
<SCRIPT type="text/vbscript" src="">
<SCRIPT type="text/javascript">
...some JavaScript...



<INPUT NAME="num" onchange="if (!checkNum(this.value, 1, 10)) {this.focus();;} else {thanks()}"  VALUE="0">
<SCRIPT type="text/javascript">

See Javascript - Script


vbs script

Event handler for a text field:

<INPUT name="edit1" size="50">    
<SCRIPT type="text/vbscript">
    Sub edit1_changed()
      If edit1.value = "abc" Then
        button1.enabled = True
        button1.enabled = False
      End If
    End Sub


Same example than above but in TCL

<INPUT name="edit1" size="50">
<SCRIPT type="text/tcl">
  proc edit1_changed {} {
     if {[edit value] == abc} {
       button1 enable 1
     } else {
       button1 enable 0
   edit1 onChange edit1_changed

HTML Element Script Reference


Scripts should refer to an element according to its assigned name.


Scripting engines should observe the following precedence rules when identifying an element:

  • a name attribute takes precedence over an id if both are set.


Get (DOM Accessors)

The document attribute document.scripts return an HTMLCollection of the script elements in the Document.

State processing

A script element has several associated pieces of state.

Documentation / Reference

Powered by ComboStrap