Section 4.12

Scripting

5 entries from this section of the HTML Living Standard, in the order the standard presents them.

Read this section of the standard ↗

All 5 elements together

One small page that uses every element of this section. Turn on Show tags and point at any part of the page: a label tells you which element draws it. Click to jump to that element's details below.

Scripting Written for this site

Every element of this section is in the page above. Point at one to find it, click it to read its details.

<div class="app">
  <h1>Weather dashboard</h1>
  <weather-card>
    <template shadowrootmode="open">
      <style>
        .card { padding: 18px 22px; border-radius: 16px; color: #fff; background: linear-gradient(135deg, #0ea5e9, #6366f1); }
        h2 { margin: 0; font-size: 1.5rem; }
        ::slotted(p) { margin: 4px 0 0; opacity: 0.9; }
      </style>
      <div class="card">
        <h2><slot name="city">Somewhere</slot></h2>
        <slot></slot>
      </div>
    </template>
    <span slot="city">Lyon</span>
    <p>Sunny all week. This card is a custom element: its slots receive the city and this sentence.</p>
  </weather-card>

  <h2>Temperatures this week</h2>
  <canvas id="chart" width="560" height="200">A bar chart of the temperatures from Monday to Friday.</canvas>

  <ul id="days"></ul>
  <template id="day-row">
    <li><span class="day"></span><span class="temp"></span></li>
  </template>

  <noscript>
    <p>Turn on JavaScript to see the chart and the list.</p>
  </noscript>

  <script>
    const week = [["Mon", 14], ["Tue", 17], ["Wed", 19], ["Thu", 16], ["Fri", 21]];

    // The template is copied once per day to build the list.
    const row = document.getElementById("day-row");
    for (const [day, temp] of week) {
      const item = row.content.cloneNode(true);
      item.querySelector(".day").textContent = day;
      item.querySelector(".temp").textContent = `${temp} °C`;
      document.getElementById("days").append(item);
    }

    // The canvas is drawn pixel by pixel by the script.
    const canvas = document.getElementById("chart");
    const ctx = canvas.getContext("2d");
    const barWidth = canvas.width / week.length;
    week.forEach(([day, temp], i) => {
      const height = temp * 8;
      const x = i * barWidth + 16;
      ctx.fillStyle = `hsl(${220 - temp * 8} 80% 55%)`;
      ctx.beginPath();
      ctx.roundRect(x, canvas.height - height - 24, barWidth - 32, height, 10);
      ctx.fill();
      ctx.fillStyle = "#1e293b";
      ctx.font = "14px system-ui";
      ctx.fillText(day, x + 8, canvas.height - 6);
    });
  </script>
</div>

Each element in detail

<script>

Baseline: widely availablesince January 2018

WHATWG Embedded script

MDN The <script> HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code. The <script> element can also be used with other languages, such as WebGL's GLSL shader programming language and JSON.

Live preview Example from MDN
<script src="javascript.js"></script>

<script>
  alert("Hello World!");
</script>

Content model

Categories
metadata; flow; phrasing; script-supporting
Parents
head; phrasing; script-supporting
Children
script, data, or script documentation*
Interface
HTMLScriptElement

Browser support

First browser version supporting <script>
Chrome 1
Edge 12
Firefox 1
Safari 3
Chrome Android 18
Safari iOS 2

First version with support, from MDN browser-compat-data.

Attributes

type
Type of script Value: "module"; "importmap"; "speculationrules"; a valid MIME type string that is not a JavaScript MIME type essence match
nomodule
Prevents execution in user agents that support module scripts Value: Boolean attribute
async
Execute script when available, without blocking while fetching Value: Boolean attribute
defer
Defer script execution Value: Boolean attribute
crossorigin
How the element handles crossorigin requests Value: "anonymous"; "use-credentials"; the empty string
integrity
Integrity metadata used in Subresource Integrity checks [SRI] Value: Text
referrerpolicy
Referrer policy for fetches initiated by the element Value: Referrer policy
fetchpriority
Sets the priority for fetches initiated by the element Value: "auto"; "high"; "low"

Also accepts the global attributes.

<noscript>

Baseline: widely availablesince January 2018

WHATWG Fallback content for script

MDN The <noscript> HTML element defines a section of HTML to be inserted if a script type on the page is unsupported or if scripting is currently turned off in the browser.

Live preview Example from MDN
<noscript>
  <!-- anchor linking to external file -->
  <a href="https://www.mozilla.org/">External Link</a>
</noscript>
<p>Rocks!</p>

Content model

Categories
metadata; flow; phrasing
Parents
head*; phrasing*; select; optgroup
Children
varies*
Interface
HTMLElement

Browser support

First browser version supporting <noscript>
Chrome 1
Edge 12
Firefox 1
Safari 3
Chrome Android 18
Safari iOS 2

First version with support, from MDN browser-compat-data.

Attributes

No element-specific attribute.

Also accepts the global attributes.

<template>

Baseline: widely availablesince May 2018

WHATWG Template

MDN The <template> HTML element serves as a mechanism for holding HTML fragments, which can either be used later via JavaScript, generated immediately and inserted into a shadow DOM, or used as part of out-of-order patching with <template for="...">.

Live preview Example from MDN
<table id="producttable">
  <thead>
    <tr>
      <th>UPC_Code</th>
      <th>Product_Name</th>
    </tr>
  </thead>
  <tbody>
    <!-- existing data could optionally be included here -->
  </tbody>
</table>

<template id="productrow">
  <tr>
    <td class="record"></td>
    <td></td>
  </tr>
</template>

Content model

Categories
metadata; flow; phrasing; script-supporting
Parents
metadata; phrasing; script-supporting; colgroup*
Children
empty
Interface
HTMLTemplateElement

Browser support

First browser version supporting <template>
Chrome 26
Edge 13
Firefox 22
Safari 8
Chrome Android 26
Safari iOS 8

First version with support, from MDN browser-compat-data.

Attributes

for
Updates existing content Value: Text
shadowrootmode
Enables streaming declarative shadow roots Value: "open"; "closed"
shadowrootdelegatesfocus
Sets delegates focus on a declarative shadow root Value: Boolean attribute
shadowrootserializable
Sets serializable on a declarative shadow root Value: Boolean attribute
shadowrootslotassignment
Sets slot assignment on a declarative shadow root Value: "named"; "manual"
shadowrootclonable
Sets clonable on a declarative shadow root Value: Boolean attribute
shadowrootcustomelementregistry
Enables declarative shadow roots to indicate they will use a custom element registry Value: Boolean attribute

Also accepts the global attributes.

<slot>

Baseline: widely availablesince July 2022

WHATWG Shadow tree slot

MDN The <slot> HTML element is a placeholder inside a Web Component that you can fill with your own markup when the component is used. This lets you create separate DOM trees and present them together.

Live preview Example from MDN
<template id="element-details-template">
  <style>
    details {
      font-family: "Open Sans Light", "Helvetica", "Arial", sans-serif;
    }
    .name {
      font-weight: bold;
      color: #217ac0;
      font-size: 120%;
    }
    h4 {
      margin: 10px 0 -8px 0;
      background: #217ac0;
      color: white;
      padding: 2px 6px;
      border: 1px solid #cee9f9;
      border-radius: 4px;
    }
    .attributes {
      margin-left: 22px;
      font-size: 90%;
    }
    .attributes p {
      margin-left: 16px;
      font-style: italic;
    }
  </style>
  <details>
    <summary>
      <code class="name">
        &lt;<slot name="element-name">NEED NAME</slot>&gt;
      </code>
      <span class="desc"><slot name="description">NEED DESCRIPTION</slot></span>
    </summary>
    <div class="attributes">
      <h4>Attributes</h4>
      <slot name="attributes"><p>None</p></slot>
    </div>
  </details>
  <hr />
</template>

Content model

Categories
flow; phrasing
Parents
phrasing
Children
transparent
Interface
HTMLSlotElement

Browser support

First browser version supporting <slot>
Chrome 53
Edge 79
Firefox 63
Safari 10
Chrome Android 53
Safari iOS 10

First version with support, from MDN browser-compat-data.

Attributes

name
Name of shadow tree slot Value: Text

Also accepts the global attributes.

<canvas>

Baseline: widely availablesince January 2018

WHATWG Scriptable bitmap canvas

MDN Use the HTML <canvas> element with either the canvas scripting API or the WebGL API to draw graphics and animations.

Live preview Example from MDN
<canvas width="120" height="120">
  An alternative text describing what your canvas displays.
</canvas>

Content model

Categories
flow; phrasing; embedded; palpable
Parents
phrasing
Children
transparent
Interface
HTMLCanvasElement

Browser support

First browser version supporting <canvas>
Chrome 1
Edge 12
Firefox 1.5
Safari 2
Chrome Android 18
Safari iOS 1

First version with support, from MDN browser-compat-data.

Attributes

width
Horizontal dimension Value: Valid non-negative integer
height
Vertical dimension Value: Valid non-negative integer

Also accepts the global attributes.