1. 8 Web application APIs
    1. 8.1 Scripting
      1. 8.1.1 Introduction
      2. 8.1.2 Agents and agent clusters
        1. 8.1.2.1 Integration with the JavaScript agent formalism
        2. 8.1.2.2 Integration with the JavaScript agent cluster formalism
      3. 8.1.3 Realms and their counterparts
        1. 8.1.3.1 Environments
        2. 8.1.3.2 Environment settings objects
        3. 8.1.3.3 Realms, settings objects, and global objects
          1. 8.1.3.3.1 Entry
          2. 8.1.3.3.2 Incumbent
          3. 8.1.3.3.3 Current
          4. 8.1.3.3.4 Relevant
        4. 8.1.3.4 Enabling and disabling scripting
        5. 8.1.3.5 Secure contexts
      4. 8.1.4 Script processing model
        1. 8.1.4.1 Scripts
        2. 8.1.4.2 Fetching scripts
        3. 8.1.4.3 Creating scripts
        4. 8.1.4.4 Calling scripts
        5. 8.1.4.5 Killing scripts
        6. 8.1.4.6 Runtime script errors
        7. 8.1.4.7 Unhandled promise rejections
      5. 8.1.5 JavaScript specification host hooks
        1. 8.1.5.1 HostEnsureCanCompileStrings(callerRealm, calleeRealm)
        2. 8.1.5.2 HostPromiseRejectionTracker(promise, operation)
        3. 8.1.5.3 Job-related host hooks
          1. 8.1.5.3.1 HostCallJobCallback(callback, V, argumentsList)
          2. 8.1.5.3.2 HostEnqueueFinalizationRegistryCleanupJob(finalizationRegistry)
          3. 8.1.5.3.3 HostEnqueuePromiseJob(job, realm)
          4. 8.1.5.3.4 HostMakeJobCallback(callable)
        4. 8.1.5.4 Module-related host hooks
          1. 8.1.5.4.1 HostGetImportMetaProperties(moduleRecord)
          2. 8.1.5.4.2 HostImportModuleDynamically(referencingScriptOrModule, moduleRequest, promiseCapability)
          3. 8.1.5.4.3 HostResolveImportedModule(referencingScriptOrModule, moduleRequest)
          4. 8.1.5.4.4 HostGetSupportedImportAssertions()
      6. 8.1.6 Event loops
        1. 8.1.6.1 Definitions
        2. 8.1.6.2 Queuing tasks
        3. 8.1.6.3 Processing model
        4. 8.1.6.4 Generic task sources
        5. 8.1.6.5 Dealing with the event loop from other specifications
      7. 8.1.7 Events
        1. 8.1.7.1 Event handlers
        2. 8.1.7.2 Event handlers on elements, Document objects, and Window objects
          1. 8.1.7.2.1 IDL definitions
        3. 8.1.7.3 Event firing
    2. 8.2 The WindowOrWorkerGlobalScope mixin
    3. 8.3 Base64 utility methods

8 Web application APIs

8.1 Scripting

8.1.1 Introduction

Various mechanisms can cause author-provided executable code to run in the context of a document. These mechanisms include, but are probably not limited to:

8.1.2 Agents and agent clusters

8.1.2.1 Integration with the JavaScript agent formalism

JavaScript defines the concept of an agent. This section gives the mapping of that language-level concept on to the web platform.

Conceptually, the agent concept is an architecture-independent, idealized "thread" in which JavaScript code runs. Such code can involve multiple globals/realms that can synchronously access each other, and thus needs to run in a single execution thread.

Two Window objects having the same agent does not indicate they can directly access all objects created in each other's realms. They would have to be same origin-domain; see IsPlatformObjectSameOrigin.

The following types of agents exist on the web platform:

Similar-origin window agent

Contains various Window objects which can potentially reach each other, either directly or by using document.domain.

If the encompassing agent cluster's is origin-keyed is true, then all the Window objects will be same origin, can reach each other directly, and document.domain will no-op.

Two Window objects that are same origin can be in different similar-origin window agents, for instance if they are each in their own browsing context group.

Dedicated worker agent

Contains a single DedicatedWorkerGlobalScope.

Shared worker agent

Contains a single SharedWorkerGlobalScope.

Service worker agent

Contains a single ServiceWorkerGlobalScope.

Worklet agent

Contains a single WorkletGlobalScope object.

Although a given worklet can have multiple realms, each such realm needs its own agent, as each realm can be executing code independently and at the same time as the others.

Only shared and dedicated worker agents allow the use of JavaScript Atomics APIs to potentially block.

To create an agent, given a boolean canBlock:

  1. Let signifier be a new unique internal value.

  2. Let candidateExecution be a new candidate execution.

  3. Let agent be a new agent whose [[CanBlock]] is canBlock, [[Signifier]] is signifier, [[CandidateExecution]] is candidateExecution, and [[IsLockFree1]], [[IsLockFree2]], and [[LittleEndian]] are set at the implementation's discretion.

  4. Set agent's event loop to a new event loop.

  5. Return agent.

The relevant agent for a platform object platformObject is platformObject's relevant Realm's agent. This pointer is not yet defined in the JavaScript specification; see tc39/ecma262#1357.

The agent equivalent of the current Realm Record is the surrounding agent.

8.1.2.2 Integration with the JavaScript agent cluster formalism

JavaScript also defines the concept of an agent cluster, which this standard maps to the web platform by placing agents appropriately when they are created using the obtain a similar-origin window agent or obtain a worker/worklet agent algorithms.

The agent cluster concept is crucial for defining the JavaScript memory model, and in particular among which agents the backing data of SharedArrayBuffer objects can be shared.

Conceptually, the agent cluster concept is an architecture-independent, idealized "process boundary" that groups together multiple "threads" (agents). The agent clusters defined by the specification are generally more restrictive than the actual process boundaries implemented in user agents. By enforcing these idealized divisions at the specification level, we ensure that web developers see interoperable behavior with regard to shared memory, even in the face of varying and changing user agent process models.

An agent cluster has an associated cross-origin isolation mode, which is a cross-origin isolation mode. It is initially "none".

An agent cluster has an associated is origin-keyed (a boolean), which is initially false.


The following defines the allocation of the agent clusters of similar-origin window agents.

An agent cluster key is a site or tuple origin. Without web developer action to achieve origin-keyed agent clusters, it will be a site.

An equivalent formulation is that an agent cluster key can be a scheme-and-host or an origin.

To obtain a similar-origin window agent, given an origin origin, a browsing context group group, and a boolean requestsOAC, run these steps:

  1. Let site be the result of obtaining a site with origin.

  2. Let key be site.

  3. If group's cross-origin isolation mode is not "none", then set key to origin.

  4. Otherwise, if group's historical agent cluster key map[origin] exists, then set key to group's historical agent cluster key map[origin].

  5. Otherwise:

    1. If requestsOAC is true, then set key to origin.

    2. Set group's historical agent cluster key map[origin] to key.

  6. If group's agent cluster map[key] does not exist, then:

    1. Let agentCluster be a new agent cluster.

    2. Set agentCluster's cross-origin isolation mode to group's cross-origin isolation mode.

    3. Set agentCluster's is origin-keyed to true if key equals origin; otherwise false.

    4. Add the result of creating an agent, given false, to agentCluster.

    5. Set group's agent cluster map[key] to agentCluster.

  7. Return the single similar-origin window agent contained in group's agent cluster map[key].

This means that there is only one similar-origin window agent per browsing context agent cluster. (However, dedicated worker and worklet agents might be in the same cluster.)


The following defines the allocation of the agent clusters of all other types of agents.

To obtain a worker/worklet agent, given an environment settings object or null outside settings, a boolean isTopLevel, and a boolean canBlock, run these steps:

  1. Let agentCluster be null.

  2. If isTopLevel is true, then:

    1. Set agentCluster to a new agent cluster.

    2. Set agentCluster's is origin-keyed to true.

      These workers can be considered to be origin-keyed. However, this is not exposed through any APIs (in the way that originAgentCluster exposes the origin-keyedness for windows).

  3. Otherwise:

    1. Assert: outside settings is not null.

    2. Let ownerAgent be outside settings's Realm's agent.

    3. Set agentCluster to the agent cluster which contains ownerAgent.

  4. Let agent be the result of creating an agent given canBlock.

  5. Add agent to agentCluster.

  6. Return agent.

To obtain a dedicated/shared worker agent, given an environment settings object outside settings and a boolean isShared, return the result of obtaining a worker/worklet agent given outside settings, isShared, and true.

To obtain a worklet agent, given an environment settings object outside settings, return the result of obtaining a worker/worklet agent given outside settings, false, and false.

To obtain a service worker agent, return the result of obtaining a worker/worklet agent given null, true, and false.


The following pairs of global objects are each within the same agent cluster, and thus can use SharedArrayBuffer instances to share memory with each other:

The following pairs of global objects are not within the same agent cluster, and thus cannot share memory:

8.1.3 Realms and their counterparts

The JavaScript specification introduces the realm concept, representing a global environment in which script is run. Each realm comes with an implementation-defined global object; much of this specification is devoted to defining that global object and its properties.

For web specifications, it is often useful to associate values or algorithms with a realm/global object pair. When the values are specific to a particular type of realm, they are associated directly with the global object in question, e.g., in the definition of the Window or WorkerGlobalScope interfaces. When the values have utility across multiple realms, we use the environment settings object concept.

Finally, in some cases it is necessary to track associated values before a realm/global object/environment settings object even comes into existence (for example, during navigation). These values are tracked in the environment concept.

8.1.3.1 Environments

An environment is an object that identifies the settings of a current or potential execution environment. An environment has the following fields:

An id

An opaque string that uniquely identifies this environment.

A creation URL

A URL that represents the location of the resource with which this environment is associated.

In the case of an environment settings object, this URL might be distinct from the environment settings object's responsible document's URL, due to mechanisms such as history.pushState().

A top-level creation URL

Null or a URL that represents the creation URL of the "top-level" environment. It is null for workers and worklets.

A top-level origin

A for now implementation-defined value, null, or an origin. For a "top-level" potential execution environment it is null (i.e., when there is no response yet); otherwise it is the "top-level" environment's origin. For a dedicated worker or worklet it is the top-level origin of its creator. For a shared or service worker it is an implementation-defined value.

This is distinct from the top-level creation URL's origin when sandboxing, workers, and worklets are involved.

A target browsing context

Null or a target browsing context for a navigation request.

An active service worker

Null or a service worker that controls the environment.

An execution ready flag

A flag that indicates whether the environment setup is done. It is initially unset.

Specifications may define environment discarding steps for environments. The steps take an environment as input.

The environment discarding steps are run for only a select few environments: the ones that will never become execution ready because, for example, they failed to load.

8.1.3.2 Environment settings objects

An environment settings object is an environment that additionally specifies algorithms for:

A realm execution context

A JavaScript execution context shared by all scripts that use this settings object, i.e. all scripts in a given JavaScript realm. When we run a classic script or run a module script, this execution context becomes the top of the JavaScript execution context stack, on top of which another execution context specific to the script in question is pushed. (This setup ensures ParseScript and Source Text Module Record's Evaluate know which Realm to use.)

A module map

A module map that is used when importing JavaScript modules.

A responsible document

A Document that is assigned responsibility for actions taken by the scripts that use this environment settings object.

For example, the URL of the responsible document is used to set the URL of the Document after it has been reset using document.open().

If the responsible event loop is not a window event loop, then the environment settings object has no responsible document.

An API URL character encoding

A character encoding used to encode URLs by APIs called by scripts that use this environment settings object.

An API base URL

A URL used by APIs called by scripts that use this environment settings object to parse URLs.

An origin

An origin used in security checks.

A policy container

A policy container containing policies used for security checks.

A cross-origin isolated capability

A boolean representing whether scripts that use this environment settings object are allowed to use APIs that require cross-origin isolation.

A time origin
A number used as the baseline for performance-related timestamps. [HRT]

An environment settings object also has an outstanding rejected promises weak set and an about-to-be-notified rejected promises list, used to track unhandled promise rejections. The outstanding rejected promises weak set must not create strong references to any of its members, and implementations are free to limit its size, e.g. by removing old entries from it when new ones are added.

An environment settings object's responsible event loop is its global object's relevant agent's event loop.

8.1.3.3 Realms, settings objects, and global objects

A global object is a JavaScript object that is the [[GlobalObject]] field of a JavaScript realm.

In this specification, all JavaScript realms are created with global objects that are either Window or WorkerGlobalScope objects.

There is always a 1-to-1-to-1 mapping between JavaScript realms, global objects, and environment settings objects:

To create a new JavaScript realm in an agent agent, optionally with instructions to create a global object or a global this binding (or both), the following steps are taken:

  1. Perform InitializeHostDefinedRealm() with the provided customizations for creating the global object and the global this binding.

  2. Let realm execution context be the running JavaScript execution context.

    This is the JavaScript execution context created in the previous step.

  3. Remove realm execution context from the JavaScript execution context stack.

  4. Let realm be realm execution context's Realm component.

  5. Set realm's agent to agent. This pointer is not yet defined in the JavaScript specification; see tc39/ecma262#1357.

  6. If agent's agent cluster's cross-origin isolation mode is "none", then:

    1. Let global be realm's global object.

    2. Let status be ! global.[[Delete]]("SharedArrayBuffer").

    3. Assert: status is true.

    This is done for compatibility with web content and there is some hope that this can be removed in the future. Web developers can still get at the constructor through new WebAssembly.Memory({ shared:true, initial:0, maximum:0 }).buffer.constructor.

  7. Return realm execution context.


When defining algorithm steps throughout this specification, it is often important to indicate what JavaScript realm is to be used—or, equivalently, what global object or environment settings object is to be used. In general, there are at least four possibilities:

Entry
This corresponds to the script that initiated the currently running script action: i.e., the function or script that the user agent called into when it called into author code.
Incumbent
This corresponds to the most-recently-entered author function or script on the stack, or the author function or script that originally scheduled the currently-running callback.
Current
This corresponds to the currently-running function object, including built-in user-agent functions which might not be implemented as JavaScript. (It is derived from the current JavaScript realm.)
Relevant
Every platform object has a relevant Realm, which is roughly the JavaScript realm in which it was created. When writing algorithms, the most prominent platform object whose relevant Realm might be important is the this value of the currently-running function object. In some cases, there can be other important relevant Realms, such as those of any arguments.

Note how the entry, incumbent, and current concepts are usable without qualification, whereas the relevant concept must be applied to a particular platform object.

The incumbent and entry concepts should not be used by new specifications, as they are excessively complicated and unintuitive to work with. We are working to remove almost all existing uses from the platform: see issue #1430 for incumbent, and issue #1431 for entry.

In general, web platform specifications should use the relevant concept, applied to the object being operated on (usually the this value of the current method). This mismatches the JavaScript specification, where current is generally used as the default (e.g. in determining the JavaScript realm whose Array constructor should be used to construct the result in Array.prototype.map). But this inconsistency is so embedded in the platform that we have to accept it going forward.

Consider the following pages, with a.html being loaded in a browser window, b.html being loaded in an iframe as shown, and c.html and d.html omitted (they can simply be empty documents):

<!-- a.html -->
<!DOCTYPE html>
<html lang="en">
<title>Entry page</title>

<iframe src="b.html"></iframe>
<button onclick="frames[0].hello()">Hello</button>

<!--b.html -->
<!DOCTYPE html>
<html lang="en">
<title>Incumbent page</title>

<iframe src="c.html" id="c"></iframe>
<iframe src="d.html" id="d"></iframe>

<script>
  const c = document.querySelector("#c").contentWindow;
  const d = document.querySelector("#d").contentWindow;

  window.hello = () => {
    c.print.call(d);
  };
</script>

Each page has its own browsing context, and thus its own JavaScript realm, global object, and environment settings object.

When the print() method is called in response to pressing the button in a.html, then:

One reason why the relevant concept is generally a better default choice than the current concept is that it is more suitable for creating an object that is to be persisted and returned multiple times. For example, the navigator.getBattery() method creates promises in the relevant Realm for the Navigator object on which it is invoked. This has the following impact: [BATTERY]

<!-- outer.html -->
<!DOCTYPE html>
<html lang="en">
<title>Relevant Realm demo: outer page</title>
<script>
  function doTest() {
    const promise = navigator.getBattery.call(frames[0].navigator);

    console.log(promise instanceof Promise);           // logs false
    console.log(promise instanceof frames[0].Promise); // logs true

    frames[0].hello();
  }
</script>
<iframe src="inner.html" onload="doTest()"></iframe>

<!-- inner.html -->
<!DOCTYPE html>
<html lang="en">
<title>Relevant Realm demo: inner page</title>
<script>
  function hello() {
    const promise = navigator.getBattery();

    console.log(promise instanceof Promise);        // logs true
    console.log(promise instanceof parent.Promise); // logs false
  }
</script>

If the algorithm for the getBattery() method had instead used the current Realm, all the results would be reversed. That is, after the first call to getBattery() in outer.html, the Navigator object in inner.html would be permanently storing a Promise object created in outer.html's JavaScript realm, and calls like that inside the hello() function would thus return a promise from the "wrong" realm. Since this is undesirable, the algorithm instead uses the relevant Realm, giving the sensible results indicated in the comments above.


The rest of this section deals with formally defining the entry, incumbent, current, and relevant concepts.

8.1.3.3.1 Entry

The process of calling scripts will push or pop realm execution contexts onto the JavaScript execution context stack, interspersed with other execution contexts.

With this in hand, we define the entry execution context to be the most recently pushed item in the JavaScript execution context stack that is a realm execution context. The entry Realm is the entry execution context's Realm component.

Then, the entry settings object is the environment settings object of the entry Realm.

Similarly, the entry global object is the global object of the entry Realm.

8.1.3.3.2 Incumbent

All JavaScript execution contexts must contain, as part of their code evaluation state, a skip-when-determining-incumbent counter value, which is initially zero. In the process of preparing to run a callback and cleaning up after running a callback, this value will be incremented and decremented.

Every event loop has an associated backup incumbent settings object stack, initially empty. Roughly speaking, it is used to determine the incumbent settings object when no author code is on the stack, but author code is responsible for the current algorithm having been run in some way. The process of preparing to run a callback and cleaning up after running a callback manipulate this stack. [WEBIDL]

When Web IDL is used to invoke author code, or when HostEnqueuePromiseJob invokes a promise job, they use the following algorithms to track relevant data for determining the incumbent settings object:

To prepare to run a callback with an environment settings object settings:

  1. Push settings onto the backup incumbent settings object stack.

  2. Let context be the topmost script-having execution context.

  3. If context is not null, increment context's skip-when-determining-incumbent counter.

To clean up after running a callback with an environment settings object settings:

  1. Let context be the topmost script-having execution context.

    This will be the same as the topmost script-having execution context inside the corresponding invocation of prepare to run a callback.

  2. If context is not null, decrement context's skip-when-determining-incumbent counter.

  3. Assert: the topmost entry of the backup incumbent settings object stack is settings.

  4. Remove settings from the backup incumbent settings object stack.

Here, the topmost script-having execution context is the topmost entry of the JavaScript execution context stack that has a non-null ScriptOrModule component, or null if there is no such entry in the JavaScript execution context stack.

With all this in place, the incumbent settings object is determined as follows:

  1. Let context be the topmost script-having execution context.

  2. If context is null, or if context's skip-when-determining-incumbent counter is greater than zero, then:

    1. Assert: the backup incumbent settings object stack is not empty.

      This assert would fail if you try to obtain the incumbent settings object from inside an algorithm that was triggered neither by calling scripts nor by Web IDL invoking a callback. For example, it would trigger if you tried to obtain the incumbent settings object inside an algorithm that ran periodically as part of the event loop, with no involvement of author code. In such cases the incumbent concept cannot be used.

    2. Return the topmost entry of the backup incumbent settings object stack.

  3. Return context's Realm component's settings object.

Then, the incumbent Realm is the Realm of the incumbent settings object.

Similarly, the incumbent global object is the global object of the incumbent settings object.


The following series of examples is intended to make it clear how all of the different mechanisms contribute to the definition of the incumbent concept:

Consider the following starter example:

<!DOCTYPE html>
<iframe></iframe>
<script>
  frames[0].postMessage("some data", "*");
</script>

There are two interesting environment settings objects here: that of window, and that of frames[0]. Our concern is: what is the incumbent settings object at the time that the algorithm for postMessage() executes?

It should be that of window, to capture the intuitive notion that the author script responsible for causing the algorithm to happen is executing in window, not frames[0]. This makes sense: the window post message steps use the incumbent settings object to determine the source property of the resulting MessageEvent, and in this case window is definitely the source of the message.

Let us now explain how the steps given above give us our intuitively-desired result of window's relevant settings object.

When the window post message steps look up the incumbent settings object, the topmost script-having execution context will be that corresponding to the script element: it was pushed onto the JavaScript execution context stack as part of ScriptEvaluation during the run a classic script algorithm. Since there are no Web IDL callback invocations involved, the context's skip-when-determining-incumbent counter is zero, so it is used to determine the incumbent settings object; the result is the environment settings object of window.

(Note how the environment settings object of frames[0] is the relevant settings object of this at the time the postMessage() method is called, and thus is involved in determining the target of the message. Whereas the incumbent is used to determine the source.)

Consider the following more complicated example:

<!DOCTYPE html>
<iframe></iframe>
<script>
  const bound = frames[0].postMessage.bind(frames[0], "some data", "*");
  window.setTimeout(bound);
</script>

This example is very similar to the previous one, but with an extra indirection through Function.prototype.bind as well as setTimeout(). But, the answer should be the same: invoking algorithms asynchronously should not change the incumbent concept.

This time, the result involves more complicated mechanisms:

When bound is converted to a Web IDL callback type, the incumbent settings object is that corresponding to window (in the same manner as in our starter example above). Web IDL stores this as the resulting callback value's callback context.

When the task posted by setTimeout() executes, the algorithm for that task uses Web IDL to invoke the stored callback value. Web IDL in turn calls the above prepare to run a callback algorithm. This pushes the stored callback context onto the backup incumbent settings object stack. At this time (inside the timer task) there is no author code on the stack, so the topmost script-having execution context is null, and nothing gets its skip-when-determining-incumbent counter incremented.

Invoking the callback then calls bound, which in turn calls the postMessage() method of frames[0]. When the postMessage() algorithm looks up the incumbent settings object, there is still no author code on the stack, since the bound function just directly calls the built-in method. So the topmost script-having execution context will be null: the JavaScript execution context stack only contains an execution context corresponding to postMessage(), with no ScriptEvaluation context or similar below it.

This is where we fall back to the backup incumbent settings object stack. As noted above, it will contain as its topmost entry the relevant settings object of window. So that is what is used as the incumbent settings object while executing the postMessage() algorithm.

Consider this final, even more convoluted example:

<!-- a.html -->
<!DOCTYPE html>
<button>click me</button>
<iframe></iframe>
<script>
const bound = frames[0].location.assign.bind(frames[0].location, "https://example.com/");
document.querySelector("button").addEventListener("click", bound);
</script>
<!-- b.html -->
<!DOCTYPE html>
<iframe src="a.html"></iframe>
<script>
  const iframe = document.querySelector("iframe");
  iframe.onload = function onLoad() {
    iframe.contentWindow.document.querySelector("button").click();
  };
</script>

Again there are two interesting environment settings objects in play: that of a.html, and that of b.html. When the location.assign() method triggers the Location-object navigate algorithm, what will be the incumbent settings object? As before, it should intuitively be that of a.html: the click listener was originally scheduled by a.html, so even if something involving b.html causes the listener to fire, the incumbent responsible is that of a.html.

The callback setup is similar to the previous example: when bound is converted to a Web IDL callback type, the incumbent settings object is that corresponding to a.html, which is stored as the callback's callback context.

When the click() method is called inside b.html, it dispatches a click event on the button that is inside a.html. This time, when the prepare to run a callback algorithm executes as part of event dispatch, there is author code on the stack; the topmost script-having execution context is that of the onLoad function, whose skip-when-determining-incumbent counter gets incremented. Additionally, a.html's environment settings object (stored as the EventHandler's callback context) is pushed onto the backup incumbent settings object stack.

Now, when the Location-object navigate algorithm looks up the incumbent settings object, the topmost script-having execution context is still that of the onLoad function (due to the fact we are using a bound function as the callback). Its skip-when-determining-incumbent counter value is one, however, so we fall back to the backup incumbent settings object stack. This gives us the environment settings object of a.html, as expected.

Note that this means that even though it is the iframe inside a.html that navigates, it is a.html itself that is used as the source browsing context, which determines among other things the request client. This is perhaps the only justifiable use of the incumbent concept on the web platform; in all other cases the consequences of using it are simply confusing and we hope to one day switch them to use current or relevant as appropriate.

8.1.3.3.3 Current

The JavaScript specification defines the current Realm Record, sometimes abbreviated to the "current Realm". [JAVASCRIPT]

Then, the current settings object is the environment settings object of the current Realm Record.

Similarly, the current global object is the global object of the current Realm Record.

8.1.3.3.4 Relevant

The relevant Realm for a platform object is the value of its [[Realm]] field.

Then, the relevant settings object for a platform object o is the environment settings object of the relevant Realm for o.

Similarly, the relevant global object for a platform object o is the global object of the relevant Realm for o.

8.1.3.4 Enabling and disabling scripting

Scripting is enabled for an environment settings object settings when all of the following conditions are true:

Scripting is disabled for an environment settings object when scripting is not enabled for it, i.e., when any of the above conditions are false.


Scripting is enabled for a node node if node's node document's browsing context is non-null, and scripting is enabled for node's relevant settings object.

Scripting is disabled for a node when scripting is not enabled, i.e., when its node document's browsing context is null or when scripting is disabled for its relevant settings object.

8.1.3.5 Secure contexts

An environment environment is a secure context if the following algorithm returns true:

  1. If environment is an environment settings object, then:

    1. Let global be environment's global object.

    2. If global is a WorkerGlobalScope, then:

      1. If global's owner set[0]'s relevant settings object is a secure context, then return true.

        We only need to check the 0th item since they will necessarily all be consistent.

      2. Return false.

    3. If global is a WorkletGlobalScope, then return true.

      Worklets can only be created in secure contexts.

  2. If the result of Is url potentially trustworthy? given environment's top-level creation URL is "Potentially Trustworthy", then return true.

  3. Return false.

An environment is a non-secure context if it is not a secure context.

8.1.4 Script processing model

8.1.4.1 Scripts

A script is one of three possible structs. All scripts have:

A settings object

An environment settings object, containing various settings that are shared with other scripts in the same context.

A record

One of the following:

A parse error

A JavaScript value, which has meaning only if the record is null, indicating that the corresponding script source text could not be parsed.

This value is used for internal tracking of immediate parse errors when creating scripts, and is not to be used directly. Instead, consult the error to rethrow when determining "what went wrong" for this script.

An error to rethrow

A JavaScript value representing an error that will prevent evaluation from succeeding. It will be re-thrown by any attempts to run the script.

This could be the script's parse error, but in the case of a module script it could instead be the parse error from one of its dependencies, or an error from resolve a module specifier.

Since this exception value is provided by the JavaScript specification, we know that it is never null, so we use null to signal that no error has occurred.

Fetch options
A script fetch options, containing various options related to fetching this script or module scripts that it imports.
A base URL

A base URL used for resolving module specifiers. This will either be the URL from which the script was obtained, for external scripts, or the document base URL of the containing document, for inline scripts.

A classic script is a type of script that has the following additional item:

A muted errors boolean

A boolean which, if true, means that error information will not be provided for errors in this script. This is used to mute errors for cross-origin scripts, since that can leak private information.

A module script is another type of script. It has no additional items.

Module scripts can be classified into three types:

As CSS stylesheets and JSON documents do not import dependent modules, and do not throw exceptions on evaluation, the fetch options and base URL of CSS module scripts and JSON module scripts and are always null.

The active script is determined by the following algorithm:

  1. Let record be GetActiveScriptOrModule().

  2. If record is null, return null.

  3. Return record.[[HostDefined]].

The active script concept is so far only used by the import() feature, to determine the base URL to use for resolving relative module specifiers.

8.1.4.2 Fetching scripts

This section introduces a number of algorithms for fetching scripts, taking various necessary inputs and resulting in classic or module scripts.


Script fetch options is a struct with the following items:

cryptographic nonce

The cryptographic nonce metadata used for the initial fetch and for fetching any imported modules

integrity metadata

The integrity metadata used for the initial fetch

parser metadata

The parser metadata used for the initial fetch and for fetching any imported modules

credentials mode

The credentials mode used for the initial fetch (for module scripts) and for fetching any imported modules (for both module scripts and classic scripts)

referrer policy

The referrer policy used for the initial fetch and for fetching any imported modules

Recall that via the import() feature, classic scripts can import module scripts.

The default classic script fetch options are a script fetch options whose cryptographic nonce is the empty string, integrity metadata is the empty string, parser metadata is "not-parser-inserted", credentials mode is "same-origin", and referrer policy is the empty string.

Given a request request and a script fetch options options, we define:

set up the classic script request

Set request's cryptographic nonce metadata to options's cryptographic nonce, its integrity metadata to options's integrity metadata, its parser metadata to options's parser metadata, and its referrer policy to options's referrer policy.

set up the module script request

Set request's cryptographic nonce metadata to options's cryptographic nonce, its integrity metadata to options's integrity metadata, its parser metadata to options's parser metadata, its credentials mode to options's credentials mode, and its referrer policy to options's referrer policy.

For any given script fetch options options, the descendant script fetch options are a new script fetch options whose items all have the same values, except for the integrity metadata, which is instead the empty string.


The algorithms below can be customized by optionally supplying a custom perform the fetch hook, which takes a request and an is top-level flag. The algorithm must complete with a response (which may be a network error), either synchronously (when using fetch a classic worker-imported script) or asynchronously (otherwise). The is top-level flag will be set for all classic script fetches, and for the initial fetch when fetching an external module script graph, fetching a module worker script graph, or fetching an import() module script graph, but not for the fetches resulting from import statements encountered throughout the graph.

By default, not supplying the perform the fetch will cause the below algorithms to simply fetch the given request, with algorithm-specific customizations to the request and validations of the resulting response.

To layer your own customizations on top of these algorithm-specific ones, supply a perform the fetch hook that modifies the given request, fetches it, and then performs specific validations of the resulting response (completing with a network error if the validations fail).

The hook can also be used to perform more subtle customizations, such as keeping a cache of responses and avoiding performing a fetch at all.

Service Workers is an example of a specification that runs these algorithms with its own options for the hook. [SW]


Now for the algorithms themselves.

To fetch a classic script given a url, a settings object, some options, a CORS setting, and a character encoding, run these steps. The algorithm will asynchronously complete with either null (on failure) or a new classic script (on success).

  1. Let request be the result of creating a potential-CORS request given url, "script", and CORS setting.

  2. Set request's client to settings object.

  3. Set up the classic script request given request and options.

  4. If the caller specified custom steps to perform the fetch, perform them on request, with the is top-level flag set. Return from this algorithm, and when the custom perform the fetch steps complete with response response, run the remaining steps.

    Otherwise, fetch request. Return from this algorithm, and run the remaining steps as part of the fetch's process response for the response response.

    response can be either CORS-same-origin or CORS-cross-origin. This only affects how error reporting happens.

  5. Set response to response's unsafe response.

  6. If response's type is "error", or response's status is not an ok status, then asynchronously complete this algorithm with null, and return.

    For historical reasons, this algorithm does not include MIME type checking, unlike the other script-fetching algorithms in this section.

  7. If response's Content Type metadata, if any, specifies a character encoding, and the user agent supports that encoding, then set character encoding to that encoding (ignoring the passed-in value).

  8. Let source text be the result of decoding response's body to Unicode, using character encoding as the fallback encoding.

    The decode algorithm overrides character encoding if the file contains a BOM.

  9. Let muted errors be true if response was CORS-cross-origin, and false otherwise.

  10. Let script be the result of creating a classic script given source text, settings object, response's url, options, and muted errors.

  11. Asynchronously complete this algorithm with script.

To fetch a classic worker script given a url, a fetch client settings object, a destination, and a script settings object, run these steps. The algorithm will asynchronously complete with either null (on failure) or a new classic script (on success).

  1. Let request be a new request whose URL is url, client is fetch client settings object, destination is destination, mode is "same-origin", credentials mode is "same-origin", parser metadata is "not parser-inserted", and whose use-URL-credentials flag is set.

  2. If the caller specified custom steps to perform the fetch, perform them on request, with the is top-level flag set. Return from this algorithm, and when the custom perform the fetch steps complete with response response, run the remaining steps.

    Otherwise, fetch request. Return from this algorithm, and run the remaining steps as part of the fetch's process response for the response response.

  3. Set response to response's unsafe response.

  4. If either of the following conditions are met:

    then asynchronously complete this algorithm with null, and return.

  5. If both of the following conditions are met:

    then asynchronously complete this algorithm with null, and return.

    Other fetch schemes are exempted from MIME type checking for historical web-compatibility reasons. We might be able to tighten this in the future; see issue #3255.

  6. Let source text be the result of UTF-8 decoding response's body.

  7. Let script be the result of creating a classic script using source text, script settings object, response's url, and the default classic script fetch options.

  8. Asynchronously complete this algorithm with script.

To fetch a classic worker-imported script given a url and a settings object, run these steps. The algorithm will synchronously complete with a classic script on success, or throw an exception on failure.

  1. Let request be a new request whose URL is url, client is settings object, destination is "script", parser metadata is "not parser-inserted", synchronous flag is set, and whose use-URL-credentials flag is set.

  2. If the caller specified custom steps to perform the fetch, perform them on request, with the is top-level flag set. Let response be the result.

    Otherwise, fetch request, and let response be the result.

    Unlike other algorithms in this section, the fetching process is synchronous here. Thus any perform the fetch steps will also finish their work synchronously.

  3. Set response to response's unsafe response.

  4. If any of the following conditions are met:

    then throw a "NetworkError" DOMException.

  5. Let source text be the result of UTF-8 decoding response's body.

  6. Let muted errors be true if response was CORS-cross-origin, and false otherwise.

  7. Let script be the result of creating a classic script given source text, settings object, response's url, the default classic script fetch options, and muted errors.

  8. Return script.

To fetch an external module script graph given a url, a settings object, and some options, run these steps. The algorithm will asynchronously complete with either null (on failure) or a module script (on success).

  1. Fetch a single module script given url, settings object, "script", options, settings object, "client", and with the top-level module fetch flag set. If the caller of this algorithm specified custom perform the fetch steps, pass those along as well. Wait until the algorithm asynchronously completes with result.

  2. If result is null, asynchronously complete this algorithm with null, and return.

  3. Let visited set be « (url, "javascript") ».

  4. Fetch the descendants of and link result given settings object, destination, and visited set. When this asynchronously completes with final result, asynchronously complete this algorithm with final result.

To fetch an import() module script graph given a moduleRequest, a base URL, a settings object, and some options, run these steps. The algorithm will asynchronously complete with either null (on failure) or a module script (on success).

  1. Let url be the result of resolving a module specifier given base URL and moduleRequest.[[Specifier]].

  2. If url is failure, then asynchronously complete this algorithm with null, and return.

  3. Assert: moduleRequest.[[Assertions]] does not contain any Record entry such that entry.[[Key]] is not "type", because we only asked for "type" assertions in HostGetSupportedImportAssertions.

  4. Let moduleType be the result of running the module type from module request steps given moduleRequest.

  5. If the result of running the module type allowed steps given moduleType and settings object is false, then asynchronously complete this algorithm with null, and return.

  6. Fetch a single module script given url, settings object, "script", options, settings object, "client", moduleRequest, and with the top-level module fetch flag set. If the caller of this algorithm specified custom perform the fetch steps, pass those along as well. Wait until the algorithm asynchronously completes with result.

  7. If result is null, asynchronously complete this algorithm with null, and return.

  8. Let visited set be « (url, moduleType) ».

  9. Fetch the descendants of and link result given settings object, destination, and visited set. When this asynchronously completes with final result, asynchronously complete this algorithm with final result.

To fetch a modulepreload module script graph given a url, a destination, a settings object, and some options, run these steps. The algorithm will asynchronously complete with either null (on failure) or a module script (on success), although it will perform optional steps even after completing.

  1. Fetch a single module script given url, settings object, destination, options, settings object, "client", and with the top-level module fetch flag set. Wait until algorithm asynchronously completes with result.

  2. Asynchronously complete this algorithm with result, but do not return.

  3. If result is not null, optionally perform the following steps:

    1. Let visited set be « (url, "javascript") ».

    2. Fetch the descendants of and link result given settings object, destination, and visited set.

    Generally, performing these steps will be beneficial for performance, as it allows pre-loading the modules that will invariably be requested later, via algorithms such as fetch an external module script graph that fetch the entire graph. However, user agents might wish to skip them in bandwidth-constrained situations, or situations where the relevant fetches are already in flight.

To fetch an inline module script graph given a source text, base URL, settings object, and options, run these steps. The algorithm will asynchronously complete with either null (on failure) or a module script (on success).

  1. Let script be the result of creating a JavaScript module script using source text, settings object, base URL, and options.

  2. If script is null, asynchronously complete this algorithm with null, and return.

  3. Let visited set be an empty set.

  4. Fetch the descendants of and link script, given settings object, the destination "script", and visited set. When this asynchronously completes with final result, asynchronously complete this algorithm with final result.

To fetch a module worker script graph given a url, a fetch client settings object, a destination, a credentials mode, and a module map settings object, fetch a worklet/module worker script graph given url, fetch client settings object, destination, credentials mode, and module map settings object, asynchronously completing with the asynchronous completion result of that algorithm.

To fetch a worklet script graph given a url, a fetch client settings object, a destination, a credentials mode, a module map settings object, and a module responses map, fetch a worklet/module worker script graph given url, fetch client settings object, destination, credentials mode, and module map settings object, asynchronously completing with the asynchronous completion result of that algorithm. Use the following custom steps to perform the fetch given response:

  1. Let requestURL be request's URL.

  2. If moduleResponsesMap[requestURL] is "fetching", wait in parallel until that entry's value changes, then queue a task on the networking task source to proceed with running the following steps.

  3. If moduleResponsesMap[requestURL] exists, then asynchronously complete the perform the fetch steps with moduleResponsesMap[requestURL].

  4. Set moduleResponsesMap[requestURL] to "fetching".

  5. Fetch request. To process response for the response response:

    1. Set moduleResponsesMap[requestURL] to response.

    2. Asynchronously complete the perform the fetch steps with response.


The following algorithms are meant for internal use by this specification only as part of fetching an external module script graph or other similar concepts above, and should not be used directly by other specifications.

This diagram illustrates how these algorithms relate to the ones above, as well as to each other:

fetch an external module script graph fetch an import() module script graph fetch a modulepreload module script graph fetch an inline module script graph fetch a module worker script graph fetch a worklet script graph fetch a worklet/module worker script graph fetch the descendants of and link a module script fetch the descendants of a module script internal module script graph fetching procedure

To fetch a worklet/module worker script graph given a url, a fetch client settings object, a destination, a credentials mode, and a module map settings object, run these steps. The algorithm will asynchronously complete with either null (on failure) or a module script (on success).

  1. Let options be a script fetch options whose cryptographic nonce is the empty string, integrity metadata is the empty string, parser metadata is "not-parser-inserted", credentials mode is credentials mode, and referrer policy is the empty string.

  2. Fetch a single module script given url, fetch client settings object, destination, options, module map settings object, "client", and with the top-level module fetch flag set. If the caller of this algorithm specified custom perform the fetch steps, pass those along as well. Wait until the algorithm asynchronously completes with result.

  3. If result is null, asynchronously complete this algorithm with null, and return.

  4. Let visited set be « (url, "javascript") ».

  5. Fetch the descendants of and link result given fetch client settings object, destination, and visited set. When this asynchronously completes with final result, asynchronously complete this algorithm with final result.

To fetch the descendants of and link a module script module script, given a fetch client settings object, a destination, and a visited set, run these steps. The algorithm will asynchronously complete with either null (on failure) or with module script (on success).

  1. Fetch the descendants of module script, given fetch client settings object, destination, and visited set.

  2. Return from this algorithm, and run the following steps when fetching the descendants of a module script asynchronously completes with result.

  3. If result is null, then asynchronously complete this algorithm with result.

    In this case, there was an error fetching one or more of the descendants. We will not attempt to link.

  4. Let parse error be the result of finding the first parse error given result.

  5. If parse error is null, then:

    1. Let record be result's record.

    2. Perform record.Link().

      This step will recursively call Link on all of the module's unlinked dependencies.

      If this throws an exception, set result's error to rethrow to that exception.

  6. Otherwise, set result's error to rethrow to parse error.

  7. Asynchronously complete this algorithm with result.

To fetch the descendants of a module script module script, given a fetch client settings object, a destination, and a visited set, run these steps. The algorithm will asynchronously complete with either null (on failure) or with module script (on success).

  1. If module script's record is null, then asynchronously complete this algorithm with module script and return.

  2. Let record be module script's record.

  3. If record is not a Cyclic Module Record, or if record.[[RequestedModules]] is empty, asynchronously complete this algorithm with module script.

  4. Let moduleRequests be a new empty list.

  5. For each ModuleRequest Record requested of record.[[RequestedModules]],

    1. Let url be the result of resolving a module specifier given module script's base URL and requested.[[Specifier]].

    2. Assert: url is never failure, because resolving a module specifier must have been previously successful with these same two arguments.

    3. Let moduleType be the result of running the module type from module request steps given requested.

    4. If visited set does not contain (url, moduleType), then:

      1. Append requested to moduleRequests.

      2. Append (url, moduleType) to visited set.

  6. Let options be the descendant script fetch options for module script's fetch options.

  7. Assert: options is not null, as module script is a JavaScript module script.

  8. For each moduleRequest in moduleRequests, perform the internal module script graph fetching procedure given moduleRequest, fetch client settings object, destination, options, module script's settings object, visited set, and module script's base URL. If the caller of this algorithm specified custom perform the fetch steps, pass those along while performing the internal module script graph fetching procedure.

    These invocations of the internal module script graph fetching procedure should be performed in parallel to each other.

    If any of the invocations of the internal module script graph fetching procedure asynchronously complete with null, asynchronously complete this algorithm with null, and return.

    Otherwise, wait until all of the internal module script graph fetching procedure invocations have asynchronously completed. Asynchronously complete this algorithm with module script.

To perform the internal module script graph fetching procedure given a moduleRequest, a fetch client settings object, a destination, some options, a module map settings object, a visited set, and a referrer, perform these steps. The algorithm will asynchronously complete with either null (on failure) or a module script (on success).

  1. Let url be the result of resolving a module specifier given referrer and moduleRequest.[[Specifier]].

  2. Assert: url is never failure, because resolving a module specifier must have been previously successful with these same two arguments.

  3. Let moduleType be the result of running the module type from module request steps given moduleRequest.

  4. Assert: visited set contains (url, moduleType).

  5. Fetch a single module script given url, fetch client settings object, destination, options, module map settings object, referrer, moduleRequest, and with the top-level module fetch flag unset. If the caller of this algorithm specified custom perform the fetch steps, pass those along while fetching a single module script.

  6. Return from this algorithm, and run the following steps when fetching a single module script asynchronously completes with result:

  7. If result is null, asynchronously complete this algorithm with null, and return.

  8. Fetch the descendants of result given fetch client settings object, destination, and visited set.

  9. When the appropriate algorithm asynchronously completes with final result, asynchronously complete this algorithm with final result.

To fetch a single module script, given a url, a fetch client settings object, a destination, some options, a module map settings object, a referrer, an optional moduleRequest, and a top-level module fetch flag, run these steps. The algorithm will asynchronously complete with either null (on failure) or a module script (on success).

  1. Let moduleType be "javascript".

  2. If moduleRequest was given, then set moduleType to the result of running the module type from module request steps given moduleRequest.

  3. Assert: the result of running the module type allowed steps given moduleType and module map settings object is true. Otherwise we would not have reached this point because a failure would have been raised when inspecting moduleRequest.[[Assertions]] in create a JavaScript module script or fetch an import() module script graph.

  4. Let moduleMap be module map settings object's module map.

  5. If moduleMap[(url, moduleType)] is "fetching", wait in parallel until that entry's value changes, then queue a task on the networking task source to proceed with running the following steps.

  6. If moduleMap[(url, moduleType)] exists, asynchronously complete this algorithm with moduleMap[url / moduleType], and return.

  7. Set moduleMap[(url, moduleType)] to "fetching".

  8. Let request be a new request whose URL is url, destination is destination, mode is "cors", referrer is referrer, and client is fetch client settings object.

  9. If destination is "worker", "sharedworker", or "serviceworker", and the top-level module fetch flag is set, then set request's mode to "same-origin".

  10. Set up the module script request given request and options.

  11. If the caller specified custom steps to perform the fetch, perform them on request, setting the is top-level flag if the top-level module fetch flag is set. Return from this algorithm, and when the custom perform the fetch steps complete with response response, run the remaining steps.

    Otherwise, fetch request. Return from this algorithm, and run the remaining steps as part of the fetch's process response for the response response.

    response is always CORS-same-origin.

  12. If either of the following conditions are met:

    then set moduleMap[(url, moduleType)] to null, asynchronously complete this algorithm with null, and return.

  13. Let source text be the result of UTF-8 decoding response's body.

  14. Let MIME type be the result of extracting a MIME type from response's header list.

  15. Let module script be null.

  16. If MIME type is a JavaScript MIME type and moduleType is "javascript", then set module script to the result of creating a JavaScript module script given source text, module map settings object, response's url, and options.

  17. If the MIME type essence of MIME type is "text/css" and moduleType is "css", then set module script to the result of creating a CSS module script given source text and module map settings object.

  18. If MIME type essence is a JSON MIME type and moduleType is "json", then set module script to the result of creating a JSON module script given source text and module map settings object.

  19. Set moduleMap[(url, moduleType)] to module script, and asynchronously complete this algorithm with module script.

    It is intentional that the module map is keyed by the request URL, whereas the base URL for the module script is set to the response URL. The former is used to deduplicate fetches, while the latter is used for URL resolution.

To find the first parse error given a root moduleScript and an optional discoveredSet:

  1. Let moduleMap be moduleScript's settings object's module map.

  2. If discoveredSet was not given, let it be an empty set.

  3. Append moduleScript to discoveredSet.

  4. If moduleScript's record is null, then return moduleScript's parse error.

  5. If moduleScript's record is not a Cyclic Module Record, then return null.

  6. Let moduleRequests be the value of moduleScript's record's [[RequestedModules]] internal slot.

  7. For each moduleRequest of moduleRequests:

    1. Let childURL be the result of resolving a module specifier given moduleScript's base URL and moduleRequest.[[Specifier]]. (This will never fail, as otherwise moduleScript would have been marked as itself having a parse error.)

    2. Let moduleType be the result of running the module type from module request steps given moduleRequest.

    3. Let childModule be moduleMap[(childURL, moduleType)].

    4. Assert: childModule is a module script (i.e., it is not "fetching" or null); by now all module scripts in the graph rooted at moduleScript will have successfully been fetched.

    5. If discoveredSet already contains childModule, continue.

    6. Let childParseError be the result of finding the first parse error given childModule and discoveredSet.

    7. If childParseError is not null, return childParseError.

  8. Return null.

8.1.4.3 Creating scripts

To create a classic script, given a string source, an environment settings object settings, a URL baseURL, some script fetch options options, and an optional muted errors boolean:

  1. If muted errors was not provided, let it be false.

  2. If muted errors is true, then set baseURL to about:blank.

    When muted errors is true, baseURL is the script's CORS-cross-origin response's url, which shouldn't be exposed to JavaScript. Therefore, baseURL is sanitized here.

  3. If scripting is disabled for settings, then set source to the empty string.

  4. Let script be a new classic script that this algorithm will subsequently initialize.

  5. Set script's settings object to settings.

  6. Set script's base URL to baseURL.

  7. Set script's fetch options to options.

  8. Set script's muted errors to muted errors.

  9. Set script's parse error and error to rethrow to null.

  10. Let result be ParseScript(source, settings's Realm, script).

    Passing script as the last parameter here ensures result.[[HostDefined]] will be script.

  11. If result is a list of errors, then:

    1. Set script's parse error and its error to rethrow to result[0].

    2. Return script.

  12. Set script's record to result.

  13. Return script.

To create a JavaScript module script, given a string source, an environment settings object settings, a URL baseURL, and some script fetch options options:

  1. If scripting is disabled for settings, then set source to the empty string.

  2. Let script be a new module script that this algorithm will subsequently initialize.

  3. Set script's settings object to settings.

  4. Set script's base URL to baseURL.

  5. Set script's fetch options to options.

  6. Set script's parse error and error to rethrow to null.

  7. Let result be ParseModule(source, settings's Realm, script).

    Passing script as the last parameter here ensures result.[[HostDefined]] will be script.

  8. If result is a list of errors, then:

    1. Set script's parse error to result[0].

    2. Return script.

  9. Assert: requested.[[Assertions]] does not contain any Record entry such that entry.[[Key]] is not "type", because we only asked for "type" assertions in HostGetSupportedImportAssertions.

  10. For each ModuleRequest record requested of result.[[RequestedModules]]:

    1. Let url be the result of resolving a module specifier given script's base URL and requested.[[Specifier]].

    2. Let moduleType be the result of running the module type from module request steps given requested.

    3. If url is failure, or if the result of running the module type allowed steps given moduleType and settings is false, then:

      1. Let error be a new TypeError exception.

      2. Set script's parse error to error.

      3. Return script.

    This step is essentially validating all of the requested module specifiers and type assertions. We treat a module with unresolvable module specifiers or unsupported type assertions the same as one that cannot be parsed; in both cases, a syntactic issue makes it impossible to ever contemplate linking the module later.

  11. Set script's record to result.

  12. Return script.

To create a CSS module script, given a string source and an environment settings object settings:

  1. Let script be a new module script that this algorithm will subsequently initialize.

  2. Set script's settings object to settings.

  3. Set script's base URL and fetch options to null.

  4. Set script's parse error and error to rethrow to null.

  5. Let sheet be the result of running the steps to create a constructed CSSStyleSheet with an empty dictionary as the argument.

  6. Run the steps to synchronously replace the rules of a CSSStyleSheet on sheet given source.

    If this throws an exception, set script's parse error to that exception, and return script.

    The steps to synchronously replace the rules of a CSSStyleSheet will throw if source contains any @import rules. This is by-design for now because there is not yet an agreement on how to handle these for CSS module scripts; therefore they are blocked altogether until a consensus is reached.

  7. Set script's record to the result of CreateDefaultExportSyntheticModule(sheet).

  8. Return script.

To create a JSON module script, given a string source and an environment settings object settings:

  1. Let script be a new module script that this algorithm will subsequently initialize.

  2. Set script's settings object to settings.

  3. Set script's base URL and fetch options to null.

  4. Set script's parse error and error to rethrow to null.

  5. Let result be ParseJSONModule(source).

    If this throws an exception, set script's parse error to that exception, and return script.

  6. Set script's record to result.

  7. Return script.

The module type from module request steps, given a ModuleRequest Record moduleRequest, are as follows:

  1. Let moduleType be "javascript".

  2. If moduleRequest.[[Assertions]] has a Record entry such that entry.[[Key]] is "type", then:

    1. If entry.[[Value]] is "javascript", then set moduleType to null.

      This specification uses the "javascript" module type internally for JavaScript module scripts, so this step is needed to prevent modules from being imported using a "javascript" type assertion (a null moduleType will cause the module type allowed check to fail).

    2. Otherwise, set moduleType to entry.[[Value]].

  3. Return moduleType.

The module type allowed steps, given a string moduleType and an environment settings object settings, are as follows:

  1. If moduleType is not "javascript", "css", or "json", then return false.

  2. If moduleType is "css" and the CSSStyleSheet interface is not exposed in settings's Realm, then return false.

  3. Return true.

8.1.4.4 Calling scripts

To run a classic script given a classic script script and an optional boolean rethrow errors (default false):

  1. Let settings be the settings object of script.

  2. Check if we can run script with settings. If this returns "do not run" then return NormalCompletion(empty).

  3. Prepare to run script given settings.

  4. Let evaluationStatus be null.

  5. If script's error to rethrow is not null, then set evaluationStatus to Completion { [[Type]]: throw, [[Value]]: script's error to rethrow, [[Target]]: empty }.

  6. Otherwise, set evaluationStatus to ScriptEvaluation(script's record).

    If ScriptEvaluation does not complete because the user agent has aborted the running script, leave evaluationStatus as null.

  7. If evaluationStatus is an abrupt completion, then:

    1. If rethrow errors is true and script's muted errors is false, then:

      1. Clean up after running script with settings.

      2. Rethrow evaluationStatus.[[Value]].

    2. If rethrow errors is true and script's muted errors is true, then:

      1. Clean up after running script with settings.

      2. Throw a "NetworkError" DOMException.

    3. Otherwise, rethrow errors is false. Perform the following steps:

      1. Report the exception given by evaluationStatus.[[Value]] for script.

      2. Clean up after running script with settings.

      3. Return evaluationStatus.

  8. Clean up after running script with settings.

  9. If evaluationStatus is a normal completion, then return evaluationStatus.

  10. If we've reached this point, evaluationStatus was left as null because the script was aborted prematurely during evaluation. Return Completion { [[Type]]: throw, [[Value]]: a new "QuotaExceededError" DOMException, [[Target]]: empty }.

To run a module script given a module script script and an optional boolean preventErrorReporting (default false):

  1. Let settings be the settings object of script.

  2. Check if we can run script with settings. If this returns "do not run", then return a promise resolved with undefined.

  3. Prepare to run script given settings.

  4. Let evaluationPromise be null.

  5. If script's error to rethrow is not null, then set evaluationPromise to a promise rejected with script's error to rethrow.

  6. Otherwise:

    1. Let record be script's record.

    2. Set evaluationPromise to record.Evaluate().

      This step will recursively evaluate all of the module's dependencies.

      If Evaluate fails to complete as a result of the user agent aborting the running script, then set evaluationPromise to a promise rejected with a new "QuotaExceededError" DOMException.

  7. If preventErrorReporting is false, then upon rejection of evaluationPromise with reason, report the exception given by reason for script.

  8. Clean up after running script with settings.

  9. Return evaluationPromise.

The steps to check if we can run script with an environment settings object settings are as follows. They return either "run" or "do not run".

  1. If the global object specified by settings is a Window object whose Document object is not fully active, then return "do not run".

  2. If scripting is disabled for settings, then return "do not run".

  3. Return "run".

The steps to prepare to run script with an environment settings object settings are as follows:

  1. Push settings's realm execution context onto the JavaScript execution context stack; it is now the running JavaScript execution context.

  2. Add settings to the currently running task's script evaluation environment settings object set.

The steps to clean up after running script with an environment settings object settings are as follows:

  1. Assert: settings's realm execution context is the running JavaScript execution context.

  2. Remove settings's realm execution context from the JavaScript execution context stack.

  3. If the JavaScript execution context stack is now empty, perform a microtask checkpoint. (If this runs scripts, these algorithms will be invoked reentrantly.)

These algorithms are not invoked by one script directly calling another, but they can be invoked reentrantly in an indirect manner, e.g. if a script dispatches an event which has event listeners registered.

The running script is the script in the [[HostDefined]] field in the ScriptOrModule component of the running JavaScript execution context.

8.1.4.5 Killing scripts

Although the JavaScript specification does not account for this possibility, it's sometimes necessary to abort a running script. This causes any ScriptEvaluation or Source Text Module Record Evaluate invocations to cease immediately, emptying the JavaScript execution context stack without triggering any of the normal mechanisms like finally blocks. [JAVASCRIPT]

User agents may impose resource limitations on scripts, for example CPU quotas, memory limits, total execution time limits, or bandwidth limitations. When a script exceeds a limit, the user agent may either throw a "QuotaExceededError" DOMException, abort the script without an exception, prompt the user, or throttle script execution.

For example, the following script never terminates. A user agent could, after waiting for a few seconds, prompt the user to either terminate the script or let it continue.

<script>
 while (true) { /* loop */ }
</script>

User agents are encouraged to allow users to disable scripting whenever the user is prompted either by a script (e.g. using the window.alert() API) or because of a script's actions (e.g. because it has exceeded a time limit).

If scripting is disabled while a script is executing, the script should be terminated immediately.

User agents may allow users to specifically disable scripts just for the purposes of closing a browsing context.

For example, the prompt mentioned in the example above could also offer the user with a mechanism to just close the page entirely, without running any unload event handlers.

8.1.4.6 Runtime script errors
self.reportError(e)

Dispatches an error event at the global object for the given value e, in the same fashion as an unhandled exception.

When the user agent is required to report an error for a particular script script with a particular position line:col, using a particular target target, it must run these steps, after which the error is either handled or not handled:

  1. If target is in error reporting mode, then return; the error is not handled.

  2. Let target be in error reporting mode.

  3. (This is a tracking vector.) Let message be an implementation-defined string describing the error in a helpful manner.

  4. Let errorValue be the value that represents the error: in the case of an uncaught exception, that would be the value that was thrown; in the case of a JavaScript error that would be an Error object. If there is no corresponding value, then the null value must be used instead.

  5. Let urlString be the result of applying the URL serializer to the URL record that corresponds to the resource from which script was obtained.

    The resource containing the script will typically be the file from which the Document was parsed, e.g. for inline script elements or event handler content attributes; or the JavaScript file that the script was in, for external scripts. Even for dynamically-generated scripts, user agents are strongly encouraged to attempt to keep track of the original source of a script. For example, if an external script uses the document.write() API to insert an inline script element during parsing, the URL of the resource containing the script would ideally be reported as being the external script, and the line number might ideally be reported as the line with the document.write() call or where the string passed to that call was first constructed. Naturally, implementing this can be somewhat non-trivial.

    User agents are similarly encouraged to keep careful track of the original line numbers, even in the face of document.write() calls mutating the document as it is parsed, or event handler content attributes spanning multiple lines.

  6. If script's muted errors is true, then set message to "Script error.", urlString to the empty string, line and col to 0, and errorValue to null.

  7. Let notHandled be true.

  8. If target implements EventTarget, then set notHandled to the result of firing an event named error at target, using ErrorEvent, with the cancelable attribute initialized to true, the message attribute initialized to message, the filename attribute initialized to urlString, the lineno attribute initialized to line, the colno attribute initialized to col, and the error attribute initialized to errorValue.

  9. Let target no longer be in error reporting mode.

  10. If notHandled is false, then the error is handled. Otherwise, the error is not handled.

    Returning true in an event handler cancels the event per the event handler processing algorithm.

When the user agent is to report an exception E, the user agent must report the error for the relevant script, with the problematic position (line number and column number) in the resource containing the script, using the global object specified by the script's settings object as the target. If the error is still not handled after this, then the error may be reported to a developer console.

The existence of both report an error and report an exception is confusing, and both algorithms have known problems. You can track future cleanup in this area in issue #958.

The reportError(e) method steps are to report the exception e.

ErrorEvent

Support in all current engines.

Firefox30+Safari6+Chrome10+
Opera11+Edge79+
Edge (Legacy)12+Internet Explorer10+
Firefox Android30+Safari iOS6+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android11+

The ErrorEvent interface is defined as follows:

[Exposed=(Window,Worker)]
interface ErrorEvent : Event {
  constructor(DOMString type, optional ErrorEventInit eventInitDict = {});

  readonly attribute DOMString message;
  readonly attribute USVString filename;
  readonly attribute unsigned long lineno;
  readonly attribute unsigned long colno;
  readonly attribute any error;
};

dictionary ErrorEventInit : EventInit {
  DOMString message = "";
  USVString filename = "";
  unsigned long lineno = 0;
  unsigned long colno = 0;
  any error = null;
};

The message attribute must return the value it was initialized to. It represents the error message.

The filename attribute must return the value it was initialized to. It represents the URL of the script in which the error originally occurred.

The lineno attribute must return the value it was initialized to. It represents the line number where the error occurred in the script.

The colno attribute must return the value it was initialized to. It represents the column number where the error occurred in the script.

The error attribute must return the value it was initialized to. Where appropriate, it is set to the object representing the error (e.g., the exception object in the case of an uncaught DOM exception).

8.1.4.7 Unhandled promise rejections

Window/rejectionhandled_event

Support in all current engines.

Firefox69+Safari11+Chrome49+
Opera36+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android🔰 68+Safari iOS11.3+Chrome Android49+WebView Android49+Samsung Internet5.0+Opera Android36+

In addition to synchronous runtime script errors, scripts may experience asynchronous promise rejections, tracked via the unhandledrejection and rejectionhandled events. Tracking these rejections is done via the HostPromiseRejectionTracker abstract operation, but reporting them is defined here.

To notify about rejected promises on a given environment settings object settings object:

  1. Let list be a copy of settings object's about-to-be-notified rejected promises list.

  2. If list is empty, return.

  3. Clear settings object's about-to-be-notified rejected promises list.

  4. Let global be settings object's global object.

  5. Queue a global task on the DOM manipulation task source given global to run the following substep:

    1. For each promise p in list:

      1. If p's [[PromiseIsHandled]] internal slot is true, continue to the next iteration of the loop.

      2. Let notHandled be the result of firing an event named unhandledrejection at global, using PromiseRejectionEvent, with the cancelable attribute initialized to true, the promise attribute initialized to p, and the reason attribute initialized to the value of p's [[PromiseResult]] internal slot.

      3. If notHandled is false, then the promise rejection is handled. Otherwise, the promise rejection is not handled.

      4. If p's [[PromiseIsHandled]] internal slot is false, add p to settings object's outstanding rejected promises weak set.

This algorithm results in promise rejections being marked as handled or not handled. These concepts parallel handled and not handled script errors. If a rejection is still not handled after this, then the rejection may be reported to a developer console.

PromiseRejectionEvent

Support in all current engines.

Firefox69+Safari11+Chrome49+
Opera36+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android🔰 68+Safari iOS11.3+Chrome Android49+WebView Android49+Samsung Internet5.0+Opera Android36+

The PromiseRejectionEvent interface is defined as follows:

[Exposed=(Window,Worker)]
interface PromiseRejectionEvent : Event {
  constructor(DOMString type, PromiseRejectionEventInit eventInitDict);

  readonly attribute Promise<any> promise;
  readonly attribute any reason;
};

dictionary PromiseRejectionEventInit : EventInit {
  required Promise<any> promise;
  any reason;
};

PromiseRejectionEvent/promise

Support in all current engines.

Firefox69+Safari11+Chrome49+
Opera36+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android🔰 68+Safari iOS11.3+Chrome Android49+WebView Android49+Samsung Internet5.0+Opera Android36+

The promise attribute must return the value it was initialized to. It represents the promise which this notification is about.

PromiseRejectionEvent/reason

Support in all current engines.

Firefox69+Safari11+Chrome49+
Opera36+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android🔰 68+Safari iOS11.3+Chrome Android49+WebView Android49+Samsung Internet5.0+Opera Android36+

The reason attribute must return the value it was initialized to. It represents the rejection reason for the promise.

8.1.5 JavaScript specification host hooks

The JavaScript specification contains a number of implementation-defined abstract operations, that vary depending on the host environment. This section defines them for user agent hosts.

8.1.5.1 HostEnsureCanCompileStrings(callerRealm, calleeRealm)

JavaScript contains an implementation-defined HostEnsureCanCompileStrings(callerRealm, calleeRealm) abstract operation. User agents must use the following implementation: [JAVASCRIPT]

  1. Perform ? EnsureCSPDoesNotBlockStringCompilation(callerRealm, calleeRealm). [CSP]

8.1.5.2 HostPromiseRejectionTracker(promise, operation)

JavaScript contains an implementation-defined HostPromiseRejectionTracker(promise, operation) abstract operation. User agents must use the following implementation: [JAVASCRIPT]

  1. Let script be the running script.

  2. If script's muted errors is true, terminate these steps.

  3. Let settings object be script's settings object.

  4. If operation is "reject",

    1. Add promise to settings object's about-to-be-notified rejected promises list.

  5. If operation is "handle",

    1. If settings object's about-to-be-notified rejected promises list contains promise, then remove promise from that list and return.

    2. If settings object's outstanding rejected promises weak set does not contain promise, then return.

    3. Remove promise from settings object's outstanding rejected promises weak set.

    4. Let global be settings object's global object.

    5. Queue a global task on the DOM manipulation task source given global to fire an event named rejectionhandled at global, using PromiseRejectionEvent, with the promise attribute initialized to promise, and the reason attribute initialized to the value of promise's [[PromiseResult]] internal slot.

8.1.5.3 Job-related host hooks

Reference/Global_Objects/Promise#Incumbent_settings_object_tracking

Support in one engine only.

Firefox50+SafariNoChromeNo
OperaNoEdgeNo
Edge (Legacy)NoInternet ExplorerNo
Firefox Android50+Safari iOSNoChrome AndroidNoWebView AndroidNoSamsung InternetNoOpera AndroidNo

The JavaScript specification defines Jobs to be scheduled and run later by the host, as well as JobCallback Records which encapsulate JavaScript functions that are called as part of jobs. The JavaScript specification contains a number of implementation-defined abstract operations that lets the host define how jobs are scheduled and how JobCallbacks are handled. HTML uses these abstract operations to track the incumbent settings object in promises and FinalizationRegistry callbacks by saving and restoring the incumbent settings object and a JavaScript execution context for the active script in JobCallbacks. This section defines them for user agent hosts.

8.1.5.3.1 HostCallJobCallback(callback, V, argumentsList)

JavaScript contains an implementation-defined HostCallJobCallback(callback, V, argumentsList) abstract operation to let hosts restore state when invoking JavaScript callbacks from inside tasks. User agents must use the following implementation: [JAVASCRIPT]

  1. Let incumbent settings be callback.[[HostDefined]].[[IncumbentSettings]].

  2. Let script execution context be callback.[[HostDefined]].[[ActiveScriptContext]].

  3. Prepare to run a callback with incumbent settings.

    This affects the incumbent concept while the callback runs.

  4. If script execution context is not null, then push script execution context onto the JavaScript execution context stack.

    This affects the active script while the callback runs.

  5. Let result be Call(callback.[[Callback]], V, argumentsList).

  6. If script execution context is not null, then pop script execution context from the JavaScript execution context stack.

  7. Clean up after running a callback with incumbent settings.

  8. Return result.

8.1.5.3.2 HostEnqueueFinalizationRegistryCleanupJob(finalizationRegistry)

JavaScript has the ability to register objects with FinalizationRegistry objects, in order to schedule a cleanup action if they are found to be garbage collected. The JavaScript specification contains an implementation-defined HostEnqueueFinalizationRegistryCleanupJob(finalizationRegistry) abstract operation to schedule the cleanup action.

The timing and occurrence of cleanup work is implementation-defined in the JavaScript specification. User agents might differ in when and whether an object is garbage collected, affecting both whether the return value of the WeakRef.prototype.deref() method is undefined, and whether FinalizationRegistry cleanup callbacks occur. There are well-known cases in popular web browsers where objects are not accessible to JavaScript, but they remain retained by the garbage collector indefinitely. HTML clears kept-alive WeakRef objects in the perform a microtask checkpoint algorithm. Authors would be best off not depending on the timing details of garbage collection implementations.

Cleanup actions do not take place interspersed with synchronous JavaScript execution, but rather happen in queued tasks. User agents must use the following implementation: [JAVASCRIPT]

  1. Let global be finalizationRegistry.[[Realm]]'s global object.

  2. Queue a global task on the JavaScript engine task source given global to perform the following steps:

    1. Let entry be finalizationRegistry.[[CleanupCallback]].[[Callback]].[[Realm]]'s environment settings object.

    2. Check if we can run script with entry. If this returns "do not run", then return.

    3. Prepare to run script with entry.

      This affects the entry concept while the cleanup callback runs.

    4. Let result be the result of performing CleanupFinalizationRegistry(finalizationRegistry).

    5. Clean up after running script with entry.

    6. If result is an abrupt completion, then report the exception given by result.[[Value]].

8.1.5.3.3 HostEnqueuePromiseJob(job, realm)

JavaScript contains an implementation-defined HostEnqueuePromiseJob(job, realm) abstract operation to schedule Promise-related operations. HTML schedules these operations in the microtask queue. User agents must use the following implementation: [JAVASCRIPT]

  1. If realm is not null, then let job settings be the settings object for realm. Otherwise, let job settings be null.

    If realm is not null, it is the Realm of the author code that will run. When job is returned by NewPromiseReactionJob, it is the realm of the promise's handler function. When job is returned by NewPromiseResolveThenableJob, it is the realm of the then function.

    If realm is null, either no author code will run or author code is guaranteed to throw. For the former, the author may not have passed in code to run, such as in promise.then(null, null). For the latter, it is because a revoked Proxy was passed. In both cases, all the steps below that would otherwise use job settings get skipped.

  2. Queue a microtask on the surrounding agent's event loop to perform the following steps:

    1. If job settings is not null, then check if we can run script with job settings. If this returns "do not run" then return.

    2. If job settings is not null, then prepare to run script with job settings.

      This affects the entry concept while the job runs.

    3. Let result be job().

      job is an abstract closure returned by NewPromiseReactionJob or NewPromiseResolveThenableJob. The promise's handler function when job is returned by NewPromiseReactionJob, and the then function when job is returned by NewPromiseResolveThenableJob, are wrapped in JobCallback Records. HTML saves the incumbent settings object and a JavaScript execution context for to the active script in HostMakeJobCallback and restores them in HostCallJobCallback.

    4. If job settings is not null, then clean up after running script with job settings.

    5. If result is an abrupt completion, then report the exception given by result.[[Value]].

8.1.5.3.4 HostMakeJobCallback(callable)

JavaScript contains an implementation-defined HostMakeJobCallback(callable) abstract operation to let hosts attach state to JavaScript callbacks that are called from inside tasks. User agents must use the following implementation: [JAVASCRIPT]

  1. Let incumbent settings be the incumbent settings object.

  2. Let active script be the active script.

  3. Let script execution context be null.

  4. If active script is not null, set script execution context to a new JavaScript execution context, with its Function field set to null, its Realm field set to active script's settings object's Realm, and its ScriptOrModule set to active script's record.

    As seen below, this is used in order to propagate the current active script forward to the time when the job callback is invoked.

    A case where active script is non-null, and saving it in this way is useful, is the following:

    Promise.resolve('import(`./example.mjs`)').then(eval);

    Without this step (and the steps that use it in HostCallJobCallback), there would be no active script when the import() expression is evaluated, since eval() is a built-in function that does not originate from any particular script.

    With this step in place, the active script is propagated from the above code into the job, allowing import() to use the original script's base URL appropriately.

    active script can be null if the user clicks on the following button:

    <button onclick="Promise.resolve('import(`./example.mjs`)').then(eval)">Click me</button>

    In this case, the JavaScript function for the event handler will be created by the get the current value of the event handler algorithm, which creates a function with null [[ScriptOrModule]] value. Thus, when the promise machinery calls HostMakeJobCallback, there will be no active script to pass along.

    As a consequence, this means that when the import() expression is evaluated, there will still be no active script. Fortunately that is handled by our implementations of HostResolveImportedModule and HostImportModuleDynamically, by falling back to using the current settings object's API base URL.

  5. Return the JobCallback Record { [[Callback]]: callable, [[HostDefined]]: { [[IncumbentSettings]]: incumbent settings, [[ActiveScriptContext]]: script execution context } }.

8.1.5.4 Module-related host hooks

The JavaScript specification defines a syntax for modules, as well as some host-agnostic parts of their processing model. This specification defines the rest of their processing model: how the module system is bootstrapped, via the script element with type attribute set to "module", and how modules are fetched, resolved, and executed. [JAVASCRIPT]

Although the JavaScript specification speaks in terms of "scripts" versus "modules", in general this specification speaks in terms of classic scripts versus module scripts, since both of them use the script element.

modulePromise = import(specifier)

Returns a promise for the module namespace object for the module script identified by specifier. This allows dynamic importing of module scripts at runtime, instead of statically using the import statement form. The specifier will be resolved relative to the active script's base URL.

The returned promise will be rejected if an invalid specifier is given, or if a failure is encountered while fetching or evaluating the resulting module graph.

This syntax can be used inside both classic and module scripts. It thus provides a bridge into the module-script world, from the classic-script world.

url = import.meta .url

Returns the active module script's base URL.

This syntax can only be used inside module scripts.

A module map is a map keyed by tuples consisting of a URL record and a string. The URL record is the request URL at which the module was fetched, and the string indicates the type of the module (e.g. "javascript"). The module map's values are either a module script, null (used to represent failed fetches), or a placeholder value "fetching". Module maps are used to ensure that imported module scripts are only fetched, parsed, and evaluated once per Document or worker.

Since module maps are keyed by (URL, module type), the following code will create three separate entries in the module map, since it results in three different (URL, module type) tuples (all with "javascript" type):

import "https://example.com/module.mjs";
import "https://example.com/module.mjs#map-buster";
import "https://example.com/module.mjs?debug=true";

That is, URL queries and fragments can be varied to create distinct entries in the module map; they are not ignored. Thus, three separate fetches and three separate module evaluations will be performed.

In contrast, the following code would only create a single entry in the module map, since after applying the URL parser to these inputs, the resulting URL records are equal:

import "https://example.com/module2.mjs";
import "https:example.com/module2.mjs";
import "https://///example.com\\module2.mjs";
import "https://example.com/foo/../module2.mjs";

So in this second example, only one fetch and one module evaluation will occur.

Note that this behavior is the same as how shared workers are keyed by their parsed constructor url.

Since module type is also part of the module map key, the following code will create two separate entries in the module map (the type is "javascript" for the first, and "css" for the second):

<script type=module>
  import "https://example.com/module";
</script>
<script type=module>
  import "https://example.com/module" assert { type: "css" };
</script>

This can result in two separate fetches and two separate module evaluations being performed. This is a willful violation of a constraint recommended (but not required) by the import assertions specification stating that each call to HostResolveImportedModule with the same (referencingScriptOrModule, moduleRequest.[[Specifier]]) pair must return the same Module Record. [JSIMPORTASSERTIONS]

In practice, due to the as-yet-unspecified memory cache (see issue #6110) the resource may only be fetched once in WebKit and Blink-based browsers. Additionally, as long as all module types are mutually exclusive, the module type check in fetch a single module script will fail for at least one of the imports, so at most one module evaluation will occur.

The purpose of including the type in the module map key is so that an import with the wrong type assertion does not prevent a different import of the same specifier but with the correct type from succeeding.

JavaScript module scripts are the default import type when importing from another JavaScript module; that is, when an import statement lacks a type import assertion the imported module script's type will be JavaScript. Attempting to import a JavaScript resource using an import statement with a type import assertion will fail:

<script type="module">
    // All of the following will fail, assuming that the imported .mjs files are served with a
    // JavaScript MIME type. JavaScript module scripts are the default and cannot be imported with
    // any import type assertion.
    import foo from "./foo.mjs" assert { type: "javascript" };
    import foo2 from "./foo2.mjs" assert { type: "js" };
    import foo3 from "./foo3.mjs" assert { type: "" };
    await import("./foo4.mjs", { assert: { type: null } });
    await import("./foo5.mjs", { assert: { type: undefined } });
</script>

To resolve a module specifier given a URL base URL and a string specifier, perform the following steps. It will return either a URL record or failure.

  1. Apply the URL parser to specifier. If the result is not failure, return the result.

  2. If specifier does not start with the character U+002F SOLIDUS (/), the two-character sequence U+002E FULL STOP, U+002F SOLIDUS (./), or the three-character sequence U+002E FULL STOP, U+002E FULL STOP, U+002F SOLIDUS (../), return failure.

    This restriction is in place so that in the future we can allow custom module loaders to give special meaning to "bare" import specifiers, like import "jquery" or import "web/crypto". For now any such imports will fail, instead of being treated as relative URLs.

  3. Return the result of applying the URL parser to specifier with base URL.

The following are valid module specifiers according to the above algorithm:

The following are valid module specifiers according to the above algorithm, but will invariably cause failures when they are fetched:

The following are not valid module specifiers according to the above algorithm:

8.1.5.4.1 HostGetImportMetaProperties(moduleRecord)

Reference/Statements/import.meta

Support in all current engines.

Firefox62+Safari11.1+Chrome64+
Opera51+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android62+Safari iOS12+Chrome Android64+WebView Android64+Samsung Internet9.0+Opera Android47+

JavaScript contains an implementation-defined HostGetImportMetaProperties abstract operation. User agents must use the following implementation: [JAVASCRIPT]

  1. Let module script be moduleRecord.[[HostDefined]].

  2. Assert: module script's base URL is not null, as module script is a JavaScript module script.

  3. Let urlString be module script's base URL, serialized.

  4. Return « Record { [[Key]]: "url", [[Value]]: urlString } ».

8.1.5.4.2 HostImportModuleDynamically(referencingScriptOrModule, moduleRequest, promiseCapability)

JavaScript contains an implementation-defined HostImportModuleDynamically abstract operation. User agents must use the following implementation: [JAVASCRIPT]

  1. Let settings object be the current settings object.

  2. If settings object's global object implements WorkletGlobalScope or ServiceWorkerGlobalScope, then:

    1. Let completion be Completion { [[Type]]: throw, [[Value]]: a new TypeError, [[Target]]: empty }.

    2. Perform FinishDynamicImport(referencingScriptOrModule, moduleRequest, promiseCapability, completion).

    3. Return.

  3. Let base URL be settings object's API base URL.

  4. Let fetch options be the default classic script fetch options.

  5. If referencingScriptOrModule is not null, then:

    1. Let referencing script be referencingScriptOrModule.[[HostDefined]].

    2. Set settings object to referencing script's settings object.

    3. Set base URL to referencing script's base URL.

    4. Set fetch options to the descendant script fetch options for referencing script's fetch options.

    5. Assert: Neither base URL nor fetch options is null, as referencing script is a classic script or a JavaScript module script.

    As explained above for HostResolveImportedModule, in the common case, referencingScriptOrModule is non-null.

  6. Fetch an import() module script graph given moduleRequest, base URL, settings object, and fetch options. Wait until the algorithm asynchronously completes with result.

  7. Let promise be null.

  8. If result is null, then set promise to a promise rejected with a new TypeError.

  9. Otherwise, set promise to the result of running a module script given result and true.

  10. Perform FinishDynamicImport(referencingScriptOrModule, moduleRequest, promiseCapability, promise).

  11. Return undefined.

8.1.5.4.3 HostResolveImportedModule(referencingScriptOrModule, moduleRequest)

JavaScript contains an implementation-defined HostResolveImportedModule abstract operation. User agents must use the following implementation: [JAVASCRIPT]

  1. Let settings object be the current settings object.

  2. Let base URL be settings object's API base URL.

  3. If referencingScriptOrModule is not null, then:

    1. Let referencing script be referencingScriptOrModule.[[HostDefined]].

    2. Set settings object to referencing script's settings object.

    3. Set base URL to referencing script's base URL.

    4. Assert: base URL is not null, as referencing script is a classic script or a JavaScript module script.

    referencingScriptOrModule is not usually null, but will be so for event handlers per the get the current value of the event handler algorithm. For example, given:

    <button onclick="import('./foo.mjs')">Click me</button>

    If a click event occurs, then at the time the import() expression runs, GetActiveScriptOrModule will return null, which will be passed to this abstract operation when HostResolveImportedModule is called by FinishDynamicImport.

  4. Let moduleMap be settings object's module map.

  5. Let url be the result of resolving a module specifier given base URL and moduleRequest.[[Specifier]].

  6. Assert: url is never failure, because resolving a module specifier must have been previously successful with these same two arguments (either while creating the corresponding module script, or in fetch an import() module script graph).

  7. Let moduleType be the result of running the module type from module request steps given moduleRequest.

  8. Let resolved module script be moduleMap[(url, moduleType)]. (This entry must exist for us to have gotten to this point.)

  9. Assert: resolved module script is a module script (i.e., is not null or "fetching").

  10. Assert: resolved module script's record is not null.

  11. Return resolved module script's record.

8.1.5.4.4 HostGetSupportedImportAssertions()

The Import Assertions proposal contains an implementation-defined HostGetSupportedImportAssertions abstract operation. User agents must use the following implementation: [JSIMPORTASSERTIONS]

  1. Return « "type" ».

8.1.6 Event loops

8.1.6.1 Definitions

To coordinate events, user interaction, scripts, rendering, networking, and so forth, user agents must use event loops as described in this section. Each agent has an associated event loop, which is unique to that agent.

The event loop of a similar-origin window agent is known as a window event loop. The event loop of a dedicated worker agent, shared worker agent, or service worker agent is known as a worker event loop. And the event loop of a worklet agent is known as a worklet event loop.

Event loops do not necessarily correspond to implementation threads. For example, multiple window event loops could be cooperatively scheduled in a single thread.

However, for the various worker agents that are allocated with [[CanBlock]] set to true, the JavaScript specification does place requirements on them regarding forward progress, which effectively amount to requiring dedicated per-agent threads in those cases.


An event loop has one or more task queues. A task queue is a set of tasks.

Task queues are sets, not queues, because step one of the event loop processing model grabs the first runnable task from the chosen queue, instead of dequeuing the first task.

The microtask queue is not a task queue.

Tasks encapsulate algorithms that are responsible for such work as:

Events

Dispatching an Event object at a particular EventTarget object is often done by a dedicated task.

Not all events are dispatched using the task queue; many are dispatched during other tasks.

Parsing

The HTML parser tokenizing one or more bytes, and then processing any resulting tokens, is typically a task.

Callbacks

Calling a callback is often done by a dedicated task.

Using a resource

When an algorithm fetches a resource, if the fetching occurs in a non-blocking fashion then the processing of the resource once some or all of the resource is available is performed by a task.

Reacting to DOM manipulation

Some elements have tasks that trigger in response to DOM manipulation, e.g. when that element is inserted into the document.

Formally, a task is a struct which has:

Steps
A series of steps specifying the work to be done by the task.
A source
One of the task sources, used to group and serialize related tasks.
A document
A Document associated with the task, or null for tasks that are not in a window event loop.
A script evaluation environment settings object set
A set of environment settings objects used for tracking script evaluation during the task.

A task is runnable if its document is either null or fully active.

Per its source field, each task is defined as coming from a specific task source. For each event loop, every task source must be associated with a specific task queue.

Essentially, task sources are used within standards to separate logically-different types of tasks, which a user agent might wish to distinguish between. Task queues are used by user agents to coalesce task sources within a given event loop.

For example, a user agent could have one task queue for mouse and key events (to which the user interaction task source is associated), and another to which all other task sources are associated. Then, using the freedom granted in the initial step of the event loop processing model, it could give keyboard and mouse events preference over other tasks three-quarters of the time, keeping the interface responsive but not starving other task queues. Note that in this setup, the processing model still enforces that the user agent would never process events from any one task source out of order.


Each event loop has a currently running task, which is either a task or null. Initially, this is null. It is used to handle reentrancy.

Each event loop has a microtask queue, which is a queue of microtasks, initially empty. A microtask is a colloquial way of referring to a task that was created via the queue a microtask algorithm.

Each event loop has a performing a microtask checkpoint boolean, which is initially false. It is used to prevent reentrant invocation of the perform a microtask checkpoint algorithm.

Each window event loop has a DOMHighResTimeStamp last render opportunity time, initially set to zero.

Each window event loop has a DOMHighResTimeStamp last idle period start time, initially set to zero.

To get the same-loop windows for a window event loop loop, return all Window objects whose relevant agent's event loop is loop.

8.1.6.2 Queuing tasks

To queue a task on a task source source, which performs a series of steps steps, optionally given an event loop event loop and a document document:

  1. If event loop was not given, set event loop to the implied event loop.

  2. If document was not given, set document to the implied document.

  3. Let task be a new task.

  4. Set task's steps to steps.

  5. Set task's source to source.

  6. Set task's document to the document.

  7. Set task's script evaluation environment settings object set to an empty set.

  8. Let queue be the task queue to which source is associated on event loop.

  9. Append task to queue.

Failing to pass an event loop and document to queue a task means relying on the ambiguous and poorly-specified implied event loop and implied document concepts. Specification authors should either always pass these values, or use the wrapper algorithms queue a global task or queue an element task instead. Using the wrapper algorithms is recommended.

To queue a global task on a task source source, with a global object global and a series of steps steps:

  1. Let event loop be global's relevant agent's event loop.

  2. Let document be global's associated Document, if global is a Window object; otherwise null.

  3. Queue a task given source, event loop, document, and steps.

To queue an element task on a task source source, with an element element and a series of steps steps:

  1. Let global be element's relevant global object.

  2. Queue a global task given source, global, and steps.

To queue a microtask which performs a series of steps steps, optionally given an event loop event loop and a document document:

  1. If event loop was not given, set event loop to the implied event loop.

  2. If document was not given, set document to the implied document.

  3. Let microtask be a new task.

  4. Set microtask's steps to steps.

  5. Set microtask's source to the microtask task source.

  6. Set microtask's document to document.

  7. Set microtask's script evaluation environment settings object set to an empty set.

  8. Enqueue microtask on event loop's microtask queue.

It is possible for a microtask to be moved to a regular task queue, if, during its initial execution, it spins the event loop. This is the only case in which the source, document, and script evaluation environment settings object set of the microtask are consulted; they are ignored by the perform a microtask checkpoint algorithm.

The implied event loop when queuing a task is the one that can deduced from the context of the calling algorithm. This is generally unambiguous, as most specification algorithms only ever involve a single agent (and thus a single event loop). The exception is algorithms involving or specifying cross-agent communication (e.g., between a window and a worker); for those cases, the implied event loop concept must not be relied upon and specifications must explicitly provide an event loop when queuing a task or microtask.

The implied document when queuing a task on an event loop event loop is determined as follows:

  1. If event loop is not a window event loop, then return null.

  2. If the task is being queued in the context of an element, then return the element's node document.

  3. If the task is being queued in the context of a browsing context, then return the browsing context's active document.

  4. If the task is being queued by or for a script, then return the script's settings object's responsible document.

  5. Assert: this step is never reached, because one of the previous conditions must be true. Really?

Both implied event loop and implied document are vaguely-defined and have a lot of action-at-a-distance. The hope is to remove these, especially implied document. See issue #4980.

8.1.6.3 Processing model

An event loop must continually run through the following steps for as long as it exists:

  1. Let taskQueue be one of the event loop's task queues, chosen in an implementation-defined manner, with the constraint that the chosen task queue must contain at least one runnable task. If there is no such task queue, then jump to the microtasks step below.

    Remember that the microtask queue is not a task queue, so it will not be chosen in this step. However, a task queue to which the microtask task source is associated might be chosen in this step. In that case, the task chosen in the next step was originally a microtask, but it got moved as part of spinning the event loop.

  2. Let oldestTask be the first runnable task in taskQueue, and remove it from taskQueue.

  3. Set the event loop's currently running task to oldestTask.

  4. Let taskStartTime be the current high resolution time.

  5. Perform oldestTask's steps.

  6. Set the event loop's currently running task back to null.

  7. Microtasks: Perform a microtask checkpoint.

  8. Let hasARenderingOpportunity be false.

  9. Let now be the current high resolution time. [HRT]

  10. Report the task's duration by performing the following steps:

    1. Let top-level browsing contexts be an empty set.

    2. For each environment settings object settings of oldestTask's script evaluation environment settings object set, append setting's top-level browsing context to top-level browsing contexts.

    3. Report long tasks, passing in taskStartTime, now (the end time of the task), top-level browsing contexts, and oldestTask.

  11. Update the rendering: if this is a window event loop, then:

    1. Let docs be all Document objects whose relevant agent's event loop is this event loop, sorted arbitrarily except that the following conditions must be met:

      In the steps below that iterate over docs, each Document must be processed in the order it is found in the list.

    2. Rendering opportunities: Remove from docs all Document objects whose browsing context do not have a rendering opportunity.

      A browsing context has a rendering opportunity if the user agent is currently able to present the contents of the browsing context to the user, accounting for hardware refresh rate constraints and user agent throttling for performance reasons, but considering content presentable even if it's outside the viewport.

      Browsing context rendering opportunities are determined based on hardware constraints such as display refresh rates and other factors such as page performance or whether the document's visibility state is "visible". Rendering opportunities typically occur at regular intervals.

      This specification does not mandate any particular model for selecting rendering opportunities. But for example, if the browser is attempting to achieve a 60Hz refresh rate, then rendering opportunities occur at a maximum of every 60th of a second (about 16.7ms). If the browser finds that a browsing context is not able to sustain this rate, it might drop to a more sustainable 30 rendering opportunities per second for that browsing context, rather than occasionally dropping frames. Similarly, if a browsing context is not visible, the user agent might decide to drop that page to a much slower 4 rendering opportunities per second, or even less.

    3. If docs is not empty, then set hasARenderingOpportunity to true and set this event loop's last render opportunity time to taskStartTime.

    4. Unnecessary rendering: Remove from docs all Document objects which meet both of the following conditions:

    5. Remove from docs all Document objects for which the user agent believes that it's preferrable to skip updating the rendering for other reasons.

      The step labeled Rendering opportunities prevents the user agent from updating the rendering when it is unable to present new content to the user (there's no rendering opportunity).

      The step labeled Unnecessary rendering prevents the user agent from updating the rendering when there's no new content to draw.

      This step enables the user agent to prevent the steps below from running for other reasons, for example, to ensure certain tasks are executed immediately after each other, with only microtask checkpoints interleaved (and without, e.g., animation frame callbacks interleaved). Concretely, a user agent might wish to coalesce timer callbacks together, with no intermediate rendering updates.

    6. For each fully active Document in docs, flush autofocus candidates for that Document if its browsing context is a top-level browsing context.

    7. For each fully active Document in docs, run the resize steps for that Document, passing in now as the timestamp. [CSSOMVIEW]

    8. For each fully active Document in docs, run the scroll steps for that Document, passing in now as the timestamp. [CSSOMVIEW]

    9. For each fully active Document in docs, evaluate media queries and report changes for that Document, passing in now as the timestamp. [CSSOMVIEW]

    10. For each fully active Document in docs, update animations and send events for that Document, passing in now as the timestamp. [WEBANIMATIONS]

    11. For each fully active Document in docs, run the fullscreen steps for that Document, passing in now as the timestamp. [FULLSCREEN]

    12. For each fully active Document in docs, if the user agent detects that the backing storage associated with a CanvasRenderingContext2D or an OffscreenCanvasRenderingContext2D, context, has been lost, then it must run the context lost steps for each such context:

      1. Let canvas be the value of context's canvas attribute, if context is a CanvasRenderingContext2D, or the associated OffscreenCanvas object for context otherwise.

      2. Set context's context lost to true.

      3. Reset the rendering context to its default state given context.

      4. Let shouldRestore be the result of firing an event named contextlost at canvas, with the cancelable attribute initialized to true.

      5. If shouldRestore is false, then abort these steps.

      6. Attempt to restore context by creating a backing storage using context's attributes and associating them with context. If this fails, then abort these steps.

      7. Set context's context lost to false.

      8. Fire an event named contextrestored at canvas.

    13. For each fully active Document in docs, run the animation frame callbacks for that Document, passing in now as the timestamp.

    14. For each fully active Document in docs, run the update intersection observations steps for that Document, passing in now as the timestamp. [INTERSECTIONOBSERVER]

    15. Invoke the mark paint timing algorithm for each Document object in docs.

    16. For each fully active Document in docs, update the rendering or user interface of that Document and its browsing context to reflect the current state.

  12. If all of the following are true

    then:

    1. Let computeDeadline be the following steps:

      1. Let deadline be this event loop's last idle period start time plus 50.

        The cap of 50ms in the future is to ensure responsiveness to new user input within the threshold of human perception.

      2. Let hasPendingRenders be false.

      3. For each windowInSameLoop of the same-loop windows for this event loop:

        1. If windowInSameLoop's map of animation frame callbacks is not empty, or if the user agent believes that the windowInSameLoop might have pending rendering updates, set hasPendingRenders to true.

        2. Let timerCallbackEstimates be the result of getting the values of windowInSameLoop's map of active timers.

        3. For each timeoutDeadline of timerCallbackEstimates, if timeoutDeadline is less than deadline, set deadline to timeoutDeadline.

      4. If hasPendingRenders is true, then:

        1. Let nextRenderDeadline be this event loop's last render opportunity time plus (1000 divided by the current refresh rate).

          The refresh rate can be hardware- or implementation-specific. For a refresh rate of 60Hz, the nextRenderDeadline would be about 16.67ms after the last render opportunity time.

        2. If nextRenderDeadline is less than deadline, then return nextRenderDeadline.

      5. Return deadline.

    2. For each win of the same-loop windows for this event loop, perform the start an idle period algorithm for win with computeDeadline. [REQUESTIDLECALLBACK]

  13. If this is a worker event loop, then:

    1. If this event loop's agent's single realm's global object is a supported DedicatedWorkerGlobalScope and the user agent believes that it would benefit from having its rendering updated at this time, then:

      1. Let now be the current high resolution time. [HRT]

      2. Run the animation frame callbacks for that DedicatedWorkerGlobalScope, passing in now as the timestamp.

      3. Update the rendering of that dedicated worker to reflect the current state.

      Similar to the notes for updating the rendering in a window event loop, a user agent can determine the rate of rendering in the dedicated worker.

    2. If there are no tasks in the event loop's task queues and the WorkerGlobalScope object's closing flag is true, then destroy the event loop, aborting these steps, resuming the run a worker steps described in the Web workers section below.


When a user agent is to perform a microtask checkpoint:

  1. If the event loop's performing a microtask checkpoint is true, then return.

  2. Set the event loop's performing a microtask checkpoint to true.

  3. While the event loop's microtask queue is not empty:

    1. Let oldestMicrotask be the result of dequeuing from the event loop's microtask queue.

    2. Set the event loop's currently running task to oldestMicrotask.

    3. Run oldestMicrotask.

      This might involve invoking scripted callbacks, which eventually calls the clean up after running script steps, which call this perform a microtask checkpoint algorithm again, which is why we use the performing a microtask checkpoint flag to avoid reentrancy.

    4. Set the event loop's currently running task back to null.

  4. For each environment settings object whose responsible event loop is this event loop, notify about rejected promises on that environment settings object.

  5. Cleanup Indexed Database transactions.

  6. Perform ClearKeptObjects().

    When WeakRef.prototype.deref() returns an object, that object is kept alive until the next invocation of ClearKeptObjects(), after which it is again subject to garbage collection.

  7. Set the event loop's performing a microtask checkpoint to false.


When an algorithm running in parallel is to await a stable state, the user agent must queue a microtask that runs the following steps, and must then stop executing (execution of the algorithm resumes when the microtask is run, as described in the following steps):

  1. Run the algorithm's synchronous section.

  2. Resumes execution of the algorithm in parallel, if appropriate, as described in the algorithm's steps.

Steps in synchronous sections are marked with ⌛.


Algorithm steps that say to spin the event loop until a condition goal is met are equivalent to substituting in the following algorithm steps:

  1. Let task be the event loop's currently running task.

    task could be a microtask.

  2. Let task source be task's source.

  3. Let old stack be a copy of the JavaScript execution context stack.

  4. Empty the JavaScript execution context stack.

  5. Perform a microtask checkpoint.

    If task is a microtask this step will be a no-op due to performing a microtask checkpoint being true.

  6. In parallel:

    1. Wait until the condition goal is met.

    2. Queue a task on task source to:

      1. Replace the JavaScript execution context stack with old stack.

      2. Perform any steps that appear after this spin the event loop instance in the original algorithm.

        This resumes task.

  7. Stop task, allowing whatever algorithm that invoked it to resume.

    This causes the event loop's main set of steps or the perform a microtask checkpoint algorithm to continue.

Unlike other algorithms in this and other specifications, which behave similar to programming-language function calls, spin the event loop is more like a macro, which saves typing and indentation at the usage site by expanding into a series of steps and operations.

An algorithm whose steps are:

  1. Do something.

  2. Spin the event loop until awesomeness happens.

  3. Do something else.

is a shorthand which, after "macro expansion", becomes

  1. Do something.

  2. Let old stack be a copy of the JavaScript execution context stack.

  3. Empty the JavaScript execution context stack.

  4. Perform a microtask checkpoint.

  5. In parallel:

    1. Wait until awesomeness happens.

    2. Queue a task on the task source in which "do something" was done to:

      1. Replace the JavaScript execution context stack with old stack.

      2. Do something else.

Here is a more full example of the substitution, where the event loop is spun from inside a task that is queued from work in parallel. The version using spin the event loop:

  1. In parallel:

    1. Do parallel thing 1.

    2. Queue a task on the DOM manipulation task source to:

      1. Do task thing 1.

      2. Spin the event loop until awesomeness happens.

      3. Do task thing 2.

    3. Do parallel thing 2.

The fully expanded version:

  1. In parallel:

    1. Do parallel thing 1.

    2. Let old stack be null.

    3. Queue a task on the DOM manipulation task source to:

      1. Do task thing 1.

      2. Set old stack to a copy of the JavaScript execution context stack.

      3. Empty the JavaScript execution context stack.

      4. Perform a microtask checkpoint.

    4. Wait until awesomeness happens.

    5. Queue a task on the DOM manipulation task source to:

      1. Replace the JavaScript execution context stack with old stack.

      2. Do task thing 2.

    6. Do parallel thing 2.


Some of the algorithms in this specification, for historical reasons, require the user agent to pause while running a task until a condition goal is met. This means running the following steps:

  1. If necessary, update the rendering or user interface of any Document or browsing context to reflect the current state.

  2. Wait until the condition goal is met. While a user agent has a paused task, the corresponding event loop must not run further tasks, and any script in the currently running task must block. User agents should remain responsive to user input while paused, however, albeit in a reduced capacity since the event loop will not be doing anything.

Pausing is highly detrimental to the user experience, especially in scenarios where a single event loop is shared among multiple documents. User agents are encouraged to experiment with alternatives to pausing, such as spinning the event loop or even simply proceeding without any kind of suspended execution at all, insofar as it is possible to do so while preserving compatibility with existing content. This specification will happily change if a less-drastic alternative is discovered to be web-compatible.

In the interim, implementers should be aware that the variety of alternatives that user agents might experiment with can change subtle aspects of event loop behavior, including task and microtask timing. Implementations should continue experimenting even if doing so causes them to violate the exact semantics implied by the pause operation.

8.1.6.4 Generic task sources

The following task sources are used by a number of mostly unrelated features in this and other specifications.

The DOM manipulation task source

This task source is used for features that react to DOM manipulations, such as things that happen in a non-blocking fashion when an element is inserted into the document.

The user interaction task source

This task source is used for features that react to user interaction, for example keyboard or mouse input.

Events sent in response to user input (e.g. click events) must be fired using tasks queued with the user interaction task source. [UIEVENTS]

The networking task source

This task source is used for features that trigger in response to network activity.

The history traversal task source

This task source is used to queue calls to history.back() and similar APIs.

8.1.6.5 Dealing with the event loop from other specifications

Writing specifications that correctly interact with the event loop can be tricky. This is compounded by how this specification uses concurrency-model-independent terminology, so we say things like "event loop" and "in parallel" instead of using more familiar model-specific terms like "main thread" or "on a background thread".

By default, specification text generally runs on the event loop. This falls out from the formal event loop processing model, in that you can eventually trace most algorithms back to a task queued there.

The algorithm steps for any JavaScript method will be invoked by author code calling that method. And author code can only be run via queued tasks, usually originating somewhere in the script processing model.

From this starting point, the overriding guideline is that any work a specification needs to perform that would otherwise block the event loop must instead be performed in parallel with it. This includes (but is not limited to):

The next complication is that, in algorithm sections that are in parallel, you must not create or manipulate objects associated to a specific JavaScript realm, global, or environment settings object. (Stated in more familiar terms, you must not directly access main-thread artifacts from a background thread.) Doing so would create data races observable to JavaScript code, since after all, your algorithm steps are running in parallel to the JavaScript code.

You can, however, manipulate specification-level data structures and values from Infra, as those are realm-agnostic. They are never directly exposed to JavaScript without a specific conversion taking place (often via Web IDL). [INFRA] [WEBIDL]

To affect the world of observable JavaScript objects, then, you must queue a global task to perform any such manipulations. This ensures your steps are properly interleaved with respect to other things happening on the event loop. Furthermore, you must choose a task source when queuing a global task; this governs the relative order of your steps versus others. If you are unsure which task source to use, pick one of the generic task sources that sounds most applicable. Finally, you must indicate which global object your queued task is associated with; this ensures that if that global object is inactive, the task does not run.

The base primitive, on which queue a global task builds, is the queue a task algorithm. In general, queue a global task is better because it automatically picks the right event loop and, where appropriate, document. Older specifications often use queue a task combined with the implied event loop and implied document concepts, but this is discouraged.

Putting this all together, we can provide a template for a typical algorithm that needs to do work asynchronously:

  1. Do any synchronous setup work, while still on the event loop. This may include converting realm-specific JavaScript values into realm-agnostic specification-level values.

  2. Perform a set of potentially-expensive steps in parallel, operating entirely on realm-agnostic values, and producing a realm-agnostic result.

  3. Queue a global task, on a specified task source and given an appropriate global object, to convert the realm-agnostic result back into observable effects on the observable world of JavaScript objects on the event loop.

The following is an algorithm that "encrypts" a passed-in list of scalar value strings input, after parsing them as URLs:

  1. Let urls be an empty list.

  2. For each string of input:

    1. Let parsed be the result of parsing string relative to the current settings object.

    2. If parsed is failure, return a promise rejected with a "SyntaxError" DOMException.

    3. Let serialized be the result of applying the URL serializer to parsed.

    4. Append serialized to urls.

  3. Let realm be the current Realm Record.

  4. Let p be a new promise.

  5. Run the following steps in parallel:

    1. Let encryptedURLs be an empty list.

    2. For each url of urls:

      1. Wait 100 milliseconds, so that people think we're doing heavy-duty encryption.

      2. Let encrypted be a new string derived from url, whose nth code unit is equal to url's nth code unit plus 13.

      3. Append encrypted to encryptedURLs.

    3. Queue a global task on the networking task source, given realm's global object, to perform the following steps:

      1. Let array be the result of converting encryptedURLs to a JavaScript array, in realm.

      2. Resolve p with array.

  6. Return p.

Here are several things to notice about this algorithm:

(On these last two points, see also whatwg/webidl issue #135 and whatwg/webidl issue #371, where we are still mulling over the subtleties of the above promise-resolution pattern.)

Another thing to note is that, in the event this algorithm was called from a Web IDL-specified operation taking a sequence<USVString>, there was an automatic conversion from realm-specific JavaScript objects provided by the author as input, into the realm-agnostic sequence<USVString> Web IDL type, which we then treat as a list of scalar value strings. So depending on how your specification is structured, there may be other implicit steps happening on the main event loop that play a part in this whole process of getting you ready to go in parallel.

8.1.7 Events

8.1.7.1 Event handlers

Events/Event_handlers

Many objects can have event handlers specified. These act as non-capture event listeners for the object on which they are specified. [DOM]

An event handler is a struct with two items:

Event handlers are exposed in two ways.

The first way, common to all event handlers, is as an event handler IDL attribute.

The second way is as an event handler content attribute. Event handlers on HTML elements and some of the event handlers on Window objects are exposed in this way.

For both of these two ways, the event handler is exposed through a name, which is a string that always starts with "on" and is followed by the name of the event for which the handler is intended.


Most of the time, the object that exposes an event handler is the same as the object on which the corresponding event listener is added. However, the body and frameset elements expose several event handlers that act upon the element's Window object, if one exists. In either case, we call the object an event handler acts upon the target of that event handler.

To determine the target of an event handler, given an EventTarget object eventTarget on which the event handler is exposed, and an event handler name name, the following steps are taken:

  1. If eventTarget is not a body element or a frameset element, then return eventTarget.

  2. If name is not the name of an attribute member of the WindowEventHandlers interface mixin and the Window-reflecting body element event handler set does not contain name, then return eventTarget.

  3. If eventTarget's node document is not an active document, then return null.

    This could happen if this object is a body element without a corresponding Window object, for example.

    This check does not necessarily prevent body and frameset elements that are not the body element of their node document from reaching the next step. In particular, a body element created in an active document (perhaps with document.createElement()) but not connected will also have its corresponding Window object as the target of several event handlers exposed through it.

  4. Return eventTarget's node document's relevant global object.


Each EventTarget object that has one or more event handlers specified has an associated event handler map, which is a map of strings representing names of event handlers to event handlers.

When an EventTarget object that has one or more event handlers specified is created, its event handler map must be initialized such that it contains an entry for each event handler that has that object as target, with items in those event handlers set to their initial values.

The order of the entries of event handler map could be arbitrary. It is not observable through any algorithms that operate on the map.

Entries are not created in the event handler map of an object for event handlers that are merely exposed on that object, but have some other object as their targets.


An event handler IDL attribute is an IDL attribute for a specific event handler. The name of the IDL attribute is the same as the name of the event handler.

The getter of an event handler IDL attribute with name name, when called, must run these steps:

  1. Let eventTarget be the result of determining the target of an event handler given this object and name.

  2. If eventTarget is null, then return null.

  3. Return the result of getting the current value of the event handler given eventTarget and name.

The setter of an event handler IDL attribute with name name, when called, must run these steps:

  1. Let eventTarget be the result of determining the target of an event handler given this object and name.

  2. If eventTarget is null, then return.

  3. If the given value is null, then deactivate an event handler given eventTarget and name.

  4. Otherwise:

    1. Let handlerMap be eventTarget's event handler map.

    2. Let eventHandler be handlerMap[name].

    3. Set eventHandler's value to the given value.

    4. Activate an event handler given eventTarget and name.

Certain event handler IDL attributes have additional requirements, in particular the onmessage attribute of MessagePort objects.


An event handler content attribute is a content attribute for a specific event handler. The name of the content attribute is the same as the name of the event handler.

Event handler content attributes, when specified, must contain valid JavaScript code which, when parsed, would match the FunctionBody production after automatic semicolon insertion.

The following attribute change steps are used to synchronize between event handler content attributes and event handlers: [DOM]

  1. If namespace is not null, or localName is not the name of an event handler content attribute on element, then return.

  2. Let eventTarget be the result of determining the target of an event handler given element and localName.

  3. If eventTarget is null, then return.

  4. If value is null, then deactivate an event handler given eventTarget and localName.

  5. Otherwise:

    1. If the Should element's inline behavior be blocked by Content Security Policy? algorithm returns "Blocked" when executed upon element, "script attribute", and value, then return. [CSP]

    2. Let handlerMap be eventTarget's event handler map.

    3. Let eventHandler be handlerMap[localName].

    4. Let location be the script location that triggered the execution of these steps.

    5. Set eventHandler's value to the internal raw uncompiled handler value/location.

    6. Activate an event handler given eventTarget and localName.

Per the DOM Standard, these steps are run even if oldValue and value are identical (setting an attribute to its current value), but not if oldValue and value are both null (removing an attribute that doesn't currently exist). [DOM]


To deactivate an event handler given an EventTarget object eventTarget and a string name that is the name of an event handler, run these steps:

  1. Let handlerMap be eventTarget's event handler map.

  2. Let eventHandler be handlerMap[name].

  3. Set eventHandler's value to null.

  4. Let listener be eventHandler's listener.

  5. If listener is not null, then remove an event listener with eventTarget and listener.

  6. Set eventHandler's listener to null.

To erase all event listeners and handlers given an EventTarget object eventTarget, run these steps:

  1. If eventTarget has an associated event handler map, then for each nameeventHandler of eventTarget's associated event handler map, deactivate an event handler given eventTarget and name.

  2. Remove all event listeners given eventTarget.

This algorithm is used to define document.open().

To activate an event handler given an EventTarget object eventTarget and a string name that is the name of an event handler, run these steps:

  1. Let handlerMap be eventTarget's event handler map.

  2. Let eventHandler be handlerMap[name].

  3. If eventHandler's listener is not null, then return.

  4. Let callback be the result of creating a Web IDL EventListener instance representing a reference to a function of one argument that executes the steps of the event handler processing algorithm, given eventTarget, name, and its argument.

    The EventListener's callback context can be arbitrary; it does not impact the steps of the event handler processing algorithm. [DOM]

    The callback is emphatically not the event handler itself. Every event handler ends up registering the same callback, the algorithm defined below, which takes care of invoking the right code, and processing the code's return value.

  5. Let listener be a new event listener whose type is the event handler event type corresponding to eventHandler and callback is callback.

    To be clear, an event listener is different from an EventListener.

  6. Add an event listener with eventTarget and listener.

  7. Set eventHandler's listener to listener.

The event listener registration happens only if the event handler's value is being set to non-null, and the event handler is not already activated. Since listeners are called in the order they were registered, assuming no deactivation occurred, the order of event listeners for a particular event type will always be:

  1. the event listeners registered with addEventListener() before the first time the event handler's value was set to non-null

  2. then the callback to which it is currently set, if any

  3. and finally the event listeners registered with addEventListener() after the first time the event handler's value was set to non-null.

This example demonstrates the order in which event listeners are invoked. If the button in this example is clicked by the user, the page will show four alerts, with the text "ONE", "TWO", "THREE", and "FOUR" respectively.

<button id="test">Start Demo</button>
<script>
 var button = document.getElementById('test');
 button.addEventListener('click', function () { alert('ONE') }, false);
 button.setAttribute('onclick', "alert('NOT CALLED')"); // event handler listener is registered here
 button.addEventListener('click', function () { alert('THREE') }, false);
 button.onclick = function () { alert('TWO'); };
 button.addEventListener('click', function () { alert('FOUR') }, false);
</script>

However, in the following example, the event handler is deactivated after its initial activation (and its event listener is removed), before being reactivated at a later time. The page will show five alerts with "ONE", "TWO", "THREE", "FOUR", and "FIVE" respectively, in order.

<button id="test">Start Demo</button>
<script>
 var button = document.getElementById('test');
 button.addEventListener('click', function () { alert('ONE') }, false);
 button.setAttribute('onclick', "alert('NOT CALLED')"); // event handler is activated here
 button.addEventListener('click', function () { alert('TWO') }, false);
 button.onclick = null;                                 // but deactivated here
 button.addEventListener('click', function () { alert('THREE') }, false);
 button.onclick = function () { alert('FOUR'); };       // and re-activated here
 button.addEventListener('click', function () { alert('FIVE') }, false);
</script>

The interfaces implemented by the event object do not influence whether an event handler is triggered or not.

The event handler processing algorithm for an EventTarget object eventTarget, a string name representing the name of an event handler, and an Event object event is as follows:

  1. Let callback be the result of getting the current value of the event handler given eventTarget and name.

  2. If callback is null, then return.

  3. Let special error event handling be true if event is an ErrorEvent object, event's type is error, and event's currentTarget implements the WindowOrWorkerGlobalScope mixin. Otherwise, let special error event handling be false.

  4. Process the Event object event as follows:

    If special error event handling is true

    Invoke callback with five arguments, the first one having the value of event's message attribute, the second having the value of event's filename attribute, the third having the value of event's lineno attribute, the fourth having the value of event's colno attribute, the fifth having the value of event's error attribute, and with the callback this value set to event's currentTarget. Let return value be the callback's return value. [WEBIDL]

    Otherwise

    Invoke callback with one argument, the value of which is the Event object event, with the callback this value set to event's currentTarget. Let return value be the callback's return value. [WEBIDL]

    If an exception gets thrown by the callback, end these steps and allow the exception to propagate. (It will propagate to the DOM event dispatch logic, which will then report the exception.)

  5. Process return value as follows:

    If event is a BeforeUnloadEvent object and event's type is beforeunload

    In this case, the event handler IDL attribute's type will be OnBeforeUnloadEventHandler, so return value will have been coerced into either null or a DOMString.

    If return value is not null, then:

    1. Set event's canceled flag.

    2. If event's returnValue attribute's value is the empty string, then set event's returnValue attribute's value to return value.

    If special error event handling is true

    If return value is true, then set event's canceled flag.

    Otherwise

    If return value is false, then set event's canceled flag.

    If we've gotten to this "Otherwise" clause because event's type is beforeunload but event is not a BeforeUnloadEvent object, then return value will never be false, since in such cases return value will have been coerced into either null or a DOMString.


The EventHandler callback function type represents a callback used for event handlers. It is represented in Web IDL as follows:

[LegacyTreatNonObjectAsNull]
callback EventHandlerNonNull = any (Event event);
typedef EventHandlerNonNull? EventHandler;

In JavaScript, any Function object implements this interface.

For example, the following document fragment:

<body onload="alert(this)" onclick="alert(this)">

...leads to an alert saying "[object Window]" when the document is loaded, and an alert saying "[object HTMLBodyElement]" whenever the user clicks something in the page.

The return value of the function affects whether the event is canceled or not: as described above, if the return value is false, the event is canceled.

There are two exceptions in the platform, for historical reasons:

For historical reasons, the onerror handler has different arguments:

[LegacyTreatNonObjectAsNull]
callback OnErrorEventHandlerNonNull = any ((Event or DOMString) event, optional DOMString source, optional unsigned long lineno, optional unsigned long colno, optional any error);
typedef OnErrorEventHandlerNonNull? OnErrorEventHandler;
window.onerror = (message, source, lineno, colno, error) => {};

Similarly, the onbeforeunload handler has a different return value:

[LegacyTreatNonObjectAsNull]
callback OnBeforeUnloadEventHandlerNonNull = DOMString? (Event event);
typedef OnBeforeUnloadEventHandlerNonNull? OnBeforeUnloadEventHandler;

An internal raw uncompiled handler is a tuple with the following information:

When the user agent is to get the current value of the event handler given an EventTarget object eventTarget and a string name that is the name of an event handler, it must run these steps:

  1. Let handlerMap be eventTarget's event handler map.

  2. Let eventHandler be handlerMap[name].

  3. If eventHandler's value is an internal raw uncompiled handler, then:

    1. If eventTarget is an element, then let element be eventTarget, and document be element's node document. Otherwise, eventTarget is a Window object, let element be null, and document be eventTarget's associated Document.

    2. If scripting is disabled for document, then return null.

    3. Let body be the uncompiled script body in eventHandler's value.

    4. Let location be the location where the script body originated, as given by eventHandler's value.

    5. If element is not null and element has a form owner, let form owner be that form owner. Otherwise, let form owner be null.

    6. Let settings object be the relevant settings object of document.

    7. If body is not parsable as FunctionBody or if parsing detects an early error, then follow these substeps:

      1. Set eventHandler's value to null.

        This does not deactivate the event handler, which additionally removes the event handler's listener (if present).

      2. Report the error for the appropriate script and with the appropriate position (line number and column number) given by location, using settings object's global object. If the error is still not handled after this, then the error may be reported to a developer console.

      3. Return null.

    8. Push settings object's realm execution context onto the JavaScript execution context stack; it is now the running JavaScript execution context.

      This is necessary so the subsequent invocation of OrdinaryFunctionCreate takes place in the correct JavaScript Realm.

    9. Let function be the result of calling OrdinaryFunctionCreate, with arguments:

      functionPrototype
      %Function.prototype%
      sourceText
      If name is onerror and eventTarget is a Window object
      The string formed by concatenating "function ", name, "(event, source, lineno, colno, error) {", U+000A LF, body, U+000A LF, and "}".
      Otherwise
      The string formed by concatenating "function ", name, "(event) {", U+000A LF, body, U+000A LF, and "}".
      ParameterList
      If name is onerror and eventTarget is a Window object
      Let the function have five arguments, named event, source, lineno, colno, and error.
      Otherwise
      Let the function have a single argument called event.
      body
      The result of parsing body above.
      thisMode
      non-lexical-this
      scope
      1. Let realm be settings object's Realm.

      2. Let scope be realm.[[GlobalEnv]].

      3. If eventHandler is an element's event handler, then set scope to NewObjectEnvironment(document, true, scope).

        (Otherwise, eventHandler is a Window object's event handler.)

      4. If form owner is not null, then set scope to NewObjectEnvironment(form owner, true, scope).

      5. If element is not null, then set scope to NewObjectEnvironment(element, true, scope).

      6. Return scope.

    10. Remove settings object's realm execution context from the JavaScript execution context stack.

    11. Set function.[[ScriptOrModule]] to null.

      This is done because the default behavior, of associating the created function with the nearest script on the stack, can lead to path-dependent results. For example, an event handler which is first invoked by user interaction would end up with null [[ScriptOrModule]] (since then this algorithm would be first invoked when the active script is null), whereas one that is first invoked by dispatching an event from script would have its [[ScriptOrModule]] set to that script.

      Instead, we just always set [[ScriptOrModule]] to null. This is more intuitive anyway; the idea that the first script which dispatches an event is somehow responsible for the event handler code is dubious.

      In practice, this only affects the resolution of relative URLs via import(), which consult the base URL of the associated script. Nulling out [[ScriptOrModule]] means that HostResolveImportedModule and HostImportModuleDynamically will fall back to the current settings object's API base URL.

    12. Set eventHandler's value to the result of creating a Web IDL EventHandler callback function object whose object reference is function and whose callback context is settings object.

  4. Return eventHandler's value.

8.1.7.2 Event handlers on elements, Document objects, and Window objects

The following are the event handlers (and their corresponding event handler event types) that must be supported by all HTML elements, as both event handler content attributes and event handler IDL attributes; and that must be supported by all Document and Window objects, as event handler IDL attributes:

Event handler Event handler event type
onabort

GlobalEventHandlers/onabort

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
abort
onauxclick auxclick
oncancel

GlobalEventHandlers/oncancel

Support in one engine only.

FirefoxNoSafariNoChrome32+
Opera19+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox AndroidNoSafari iOSNoChrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
cancel
oncanplay

GlobalEventHandlers/oncanplay

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
canplay
oncanplaythrough

GlobalEventHandlers/oncanplaythrough

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
canplaythrough
onchange

GlobalEventHandlers/onchange

Support in all current engines.

Firefox1+Safari3+Chrome1+
Opera9+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android4+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android10.1+
change
onclick

GlobalEventHandlers/onclick

Support in all current engines.

Firefox1+Safari3+Chrome1+
Opera9+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android4+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android10.1+
click
onclose

GlobalEventHandlers/onclose

Firefox53+SafariNoChrome32+
Opera19+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android53+Safari iOSNoChrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
close
oncontextlost contextlost
oncontextmenu

GlobalEventHandlers/oncontextmenu

Support in all current engines.

Firefox9+Safari4+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer5+
Firefox Android9+Safari iOS3.2+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android12.1+
contextmenu
oncontextrestored contextrestored
oncuechange

GlobalEventHandlers/oncuechange

Support in all current engines.

Firefox68+Safari10.1+Chrome32+
Opera19+Edge79+
Edge (Legacy)18Internet ExplorerNo
Firefox Android68+Safari iOS10.3+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
cuechange
ondblclick

GlobalEventHandlers/ondblclick

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
dblclick
ondrag drag
ondragend dragend
ondragenter dragenter
ondragleave dragleave
ondragover dragover
ondragstart dragstart
ondrop drop
ondurationchange

GlobalEventHandlers/ondurationchange

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
durationchange
onemptied

GlobalEventHandlers/onemptied

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
emptied
onended

GlobalEventHandlers/onended

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
ended
onformdata

GlobalEventHandlers/onformdata

Support in all current engines.

Firefox72+Safari15+Chrome77+
Opera64+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android79+Safari iOS15+Chrome Android77+WebView Android77+Samsung Internet12.0+Opera Android55+
formdata
oninput

GlobalEventHandlers/oninput

Support in all current engines.

Firefox9+Safari4+Chrome1+
Opera10+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS3.2+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android10.1+
input
oninvalid

GlobalEventHandlers/oninvalid

Support in all current engines.

Firefox9+Safari5+Chrome4+
Opera12.1+Edge79+
Edge (Legacy)13+Internet ExplorerNo
Firefox Android9+Safari iOS4+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android12.1+
invalid
onkeydown

GlobalEventHandlers/onkeydown

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
keydown
onkeypress keypress
onkeyup

GlobalEventHandlers/onkeyup

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
keyup
onloadeddata

GlobalEventHandlers/onloadeddata

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
loadeddata
onloadedmetadata

GlobalEventHandlers/onloadedmetadata

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
loadedmetadata
onloadstart

GlobalEventHandlers/onloadstart

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
loadstart
onmousedown

GlobalEventHandlers/onmousedown

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
mousedown
onmouseenter

GlobalEventHandlers/onmouseenter

Support in all current engines.

Firefox10+Safari7+Chrome30+
Opera17+Edge79+
Edge (Legacy)12+Internet Explorer5.5+
Firefox Android10+Safari iOS7+Chrome Android30+WebView Android4.4+Samsung Internet2.0+Opera Android18+
mouseenter
onmouseleave

GlobalEventHandlers/onmouseleave

Support in all current engines.

Firefox10+Safari7+Chrome30+
Opera17+Edge79+
Edge (Legacy)12+Internet Explorer5.5+
Firefox Android10+Safari iOS7+Chrome Android30+WebView Android4.4+Samsung Internet2.0+Opera Android18+
mouseleave
onmousemove

GlobalEventHandlers/onmousemove

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
mousemove
onmouseout

GlobalEventHandlers/onmouseout

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
mouseout
onmouseover

GlobalEventHandlers/onmouseover

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
mouseover
onmouseup

GlobalEventHandlers/onmouseup

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
mouseup
onpause

GlobalEventHandlers/onpause

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
pause
onplay

GlobalEventHandlers/onplay

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
play
onplaying

GlobalEventHandlers/onplaying

Support in all current engines.

Firefox9+Safari9+Chrome32+
Opera19+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS9+Chrome Android32+WebView Android4.4.3+Samsung Internet2.0+Opera Android19+
playing
onprogress progress
onratechange ratechange
onreset

GlobalEventHandlers/onreset

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
reset
onsecuritypolicyviolation

GlobalEventHandlers/onsecuritypolicyviolation

Firefox93+Safaripreview+ChromeNo
OperaNoEdgeNo
Edge (Legacy)NoInternet ExplorerNo
Firefox Android93+Safari iOSNoChrome AndroidNoWebView AndroidNoSamsung InternetNoOpera AndroidNo
securitypolicyviolation
onseeked seeked
onseeking seeking
onselect

GlobalEventHandlers/onselect

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
select
onslotchange

GlobalEventHandlers/onslotchange

Firefox93+Safaripreview+ChromeNo
OperaNoEdgeNo
Edge (Legacy)NoInternet ExplorerNo
Firefox Android93+Safari iOSNoChrome AndroidNoWebView AndroidNoSamsung InternetNoOpera AndroidNo
slotchange
onstalled stalled
onsubmit

GlobalEventHandlers/onsubmit

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
submit
onsuspend suspend
ontimeupdate timeupdate
ontoggle toggle
onvolumechange volumechange
onwaiting waiting
onwebkitanimationend webkitAnimationEnd
onwebkitanimationiteration webkitAnimationIteration
onwebkitanimationstart webkitAnimationStart
onwebkittransitionend webkitTransitionEnd
onwheel

GlobalEventHandlers/onwheel

Support in all current engines.

Firefox17+Safari7+Chrome31+
Opera18+Edge79+
Edge (Legacy)12+Internet ExplorerNo
Firefox Android17+Safari iOS7+Chrome Android31+WebView Android4.4.3+Samsung Internet2.0+Opera Android18+
wheel

The following are the event handlers (and their corresponding event handler event types) that must be supported by all HTML elements other than body and frameset elements, as both event handler content attributes and event handler IDL attributes; that must be supported by all Document objects, as event handler IDL attributes; and that must be supported by all Window objects, as event handler IDL attributes on the Window objects themselves, and with corresponding event handler content attributes and event handler IDL attributes exposed on all body and frameset elements that are owned by that Window object's associated Document:

Event handler Event handler event type
onblur

GlobalEventHandlers/onblur

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
blur
onerror

GlobalEventHandlers/onerror

Support in all current engines.

Firefox1+Safari6+Chrome10+
Opera11.6+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android4+Safari iOS6+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android12+
error
onfocus

GlobalEventHandlers/onfocus

Support in all current engines.

Firefox9+Safari1+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
focus
onload

GlobalEventHandlers/onload

Support in all current engines.

Firefox1+Safari3+Chrome1+
Opera9+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android4+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android10.1+
load
onresize

GlobalEventHandlers/onresize

Support in all current engines.

Firefox38+Safari10.1+Chrome34+
Opera21+Edge79+
Edge (Legacy)NoInternet Explorer🔰 4+
Firefox Android38+Safari iOS10.3+Chrome Android34+WebView Android37+Samsung Internet2.0+Opera Android21+
resize
onscroll

GlobalEventHandlers/onscroll

Support in all current engines.

Firefox9+Safari1.3+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android9+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
scroll

We call the set of the names of the event handlers listed in the first column of this table the Window-reflecting body element event handler set.


The following are the event handlers (and their corresponding event handler event types) that must be supported by Window objects, as event handler IDL attributes on the Window objects themselves, and with corresponding event handler content attributes and event handler IDL attributes exposed on all body and frameset elements that are owned by that Window object's associated Document:

Event handler Event handler event type
onafterprint

WindowEventHandlers/onafterprint

Support in all current engines.

Firefox6+Safari13+Chrome63+
Opera50+Edge79+
Edge (Legacy)12+Internet Explorer6+
Firefox Android?Safari iOS13+Chrome Android63+WebView Android63+Samsung Internet8.0+Opera Android46+
afterprint
onbeforeprint

WindowEventHandlers/onbeforeprint

Support in all current engines.

Firefox6+Safari13+Chrome63+
Opera50+Edge79+
Edge (Legacy)12+Internet Explorer6+
Firefox Android?Safari iOS13+Chrome Android63+WebView Android63+Samsung Internet8.0+Opera Android46+
beforeprint
onbeforeunload

WindowEventHandlers/onbeforeunload

Support in all current engines.

Firefox1+Safari3+Chrome1+
Opera12+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android4+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12+
beforeunload
onhashchange

WindowEventHandlers/onhashchange

Support in all current engines.

Firefox3.6+Safari5+Chrome5+
Opera10+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android4+Safari iOS5+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android10.1+
hashchange
onlanguagechange

WindowEventHandlers/onlanguagechange

Support in all current engines.

Firefox32+Safari10.1+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android4+Safari iOS10.3+Chrome Android37+WebView Android37+Samsung Internet4.0+Opera Android24+
languagechange
onmessage

WindowEventHandlers/onmessage

Support in all current engines.

Firefox9+Safari4+Chrome60+
Opera47+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android9+Safari iOS4+Chrome Android60+WebView Android60+Samsung Internet8.0+Opera Android44+
message
onmessageerror

WindowEventHandlers/onmessageerror

Firefox57+SafariNoChrome60+
Opera47+Edge79+
Edge (Legacy)18Internet ExplorerNo
Firefox Android57+Safari iOSNoChrome Android60+WebView Android60+Samsung Internet8.0+Opera Android44+
messageerror
onoffline offline
ononline online
onpagehide pagehide
onpageshow pageshow
onpopstate

WindowEventHandlers/onpopstate

Support in all current engines.

Firefox4+Safari5+Chrome5+
Opera11.5+Edge79+
Edge (Legacy)12+Internet Explorer10+
Firefox Android4+Safari iOS4.2+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android11.5+
popstate
onrejectionhandled

WindowEventHandlers/onrejectionhandled

Support in all current engines.

Firefox69+Safari11+Chrome49+
Opera36+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android🔰 68+Safari iOS11.3+Chrome Android49+WebView Android49+Samsung Internet5.0+Opera AndroidNo
rejectionhandled
onstorage

WindowEventHandlers/onstorage

Support in all current engines.

Firefox45+Safari4+Chrome1+
Opera15+Edge79+
Edge (Legacy)15+Internet Explorer9+
Firefox Android45+Safari iOS4+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android14+
storage
onunhandledrejection

WindowEventHandlers/onunhandledrejection

Support in all current engines.

Firefox69+Safari11+Chrome49+
Opera36+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android🔰 68+Safari iOS11.3+Chrome Android49+WebView Android49+Samsung Internet5.0+Opera AndroidNo
unhandledrejection
onunload

WindowEventHandlers/onunload

Support in all current engines.

Firefox9+Safari4+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer6+
Firefox Android9+Safari iOS3+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android12.1+
unload

This list of event handlers is reified as event handler IDL attributes through the WindowEventHandlers interface mixin.


The following are the event handlers (and their corresponding event handler event types) that must be supported by all HTML elements, as both event handler content attributes and event handler IDL attributes; and that must be supported by all Document objects, as event handler IDL attributes:

Event handler Event handler event type
oncut cut
oncopy copy
onpaste paste

This list of event handlers is reified as event handler IDL attributes through the DocumentAndElementEventHandlers interface mixin.


The following are the event handlers (and their corresponding event handler event types) that must be supported on Document objects as event handler IDL attributes:

Event handler Event handler event type
onreadystatechange readystatechange
onvisibilitychange visibilitychange
8.1.7.2.1 IDL definitions

GlobalEventHandlers

Support in all current engines.

Firefox1+Safari1+Chrome1+
OperaYesEdge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android4+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera AndroidYes

WindowEventHandlers

Support in all current engines.

Firefox1+Safari3+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer4+
Firefox Android4+Safari iOS1+Chrome Android18+WebView Android1+Samsung Internet1.0+Opera Android12.1+
interface mixin GlobalEventHandlers {
  attribute EventHandler onabort;
  attribute EventHandler onauxclick;
  attribute EventHandler onblur;
  attribute EventHandler oncancel;
  attribute EventHandler oncanplay;
  attribute EventHandler oncanplaythrough;
  attribute EventHandler onchange;
  attribute EventHandler onclick;
  attribute EventHandler onclose;
  attribute EventHandler oncontextlost;
  attribute EventHandler oncontextmenu;
  attribute EventHandler oncontextrestored;
  attribute EventHandler oncuechange;
  attribute EventHandler ondblclick;
  attribute EventHandler ondrag;
  attribute EventHandler ondragend;
  attribute EventHandler ondragenter;
  attribute EventHandler ondragleave;
  attribute EventHandler ondragover;
  attribute EventHandler ondragstart;
  attribute EventHandler ondrop;
  attribute EventHandler ondurationchange;
  attribute EventHandler onemptied;
  attribute EventHandler onended;
  attribute OnErrorEventHandler onerror;
  attribute EventHandler onfocus;
  attribute EventHandler onformdata;
  attribute EventHandler oninput;
  attribute EventHandler oninvalid;
  attribute EventHandler onkeydown;
  attribute EventHandler onkeypress;
  attribute EventHandler onkeyup;
  attribute EventHandler onload;
  attribute EventHandler onloadeddata;
  attribute EventHandler onloadedmetadata;
  attribute EventHandler onloadstart;
  attribute EventHandler onmousedown;
  [LegacyLenientThis] attribute EventHandler onmouseenter;
  [LegacyLenientThis] attribute EventHandler onmouseleave;
  attribute EventHandler onmousemove;
  attribute EventHandler onmouseout;
  attribute EventHandler onmouseover;
  attribute EventHandler onmouseup;
  attribute EventHandler onpause;
  attribute EventHandler onplay;
  attribute EventHandler onplaying;
  attribute EventHandler onprogress;
  attribute EventHandler onratechange;
  attribute EventHandler onreset;
  attribute EventHandler onresize;
  attribute EventHandler onscroll;
  attribute EventHandler onsecuritypolicyviolation;
  attribute EventHandler onseeked;
  attribute EventHandler onseeking;
  attribute EventHandler onselect;
  attribute EventHandler onslotchange;
  attribute EventHandler onstalled;
  attribute EventHandler onsubmit;
  attribute EventHandler onsuspend;
  attribute EventHandler ontimeupdate;
  attribute EventHandler ontoggle;
  attribute EventHandler onvolumechange;
  attribute EventHandler onwaiting;
  attribute EventHandler onwebkitanimationend;
  attribute EventHandler onwebkitanimationiteration;
  attribute EventHandler onwebkitanimationstart;
  attribute EventHandler onwebkittransitionend;
  attribute EventHandler onwheel;
};

interface mixin WindowEventHandlers {
  attribute EventHandler onafterprint;
  attribute EventHandler onbeforeprint;
  attribute OnBeforeUnloadEventHandler onbeforeunload;
  attribute EventHandler onhashchange;
  attribute EventHandler onlanguagechange;
  attribute EventHandler onmessage;
  attribute EventHandler onmessageerror;
  attribute EventHandler onoffline;
  attribute EventHandler ononline;
  attribute EventHandler onpagehide;
  attribute EventHandler onpageshow;
  attribute EventHandler onpopstate;
  attribute EventHandler onrejectionhandled;
  attribute EventHandler onstorage;
  attribute EventHandler onunhandledrejection;
  attribute EventHandler onunload;
};

interface mixin DocumentAndElementEventHandlers {
  attribute EventHandler oncopy;
  attribute EventHandler oncut;
  attribute EventHandler onpaste;
};
8.1.7.3 Event firing

Certain operations and methods are defined as firing events on elements. For example, the click() method on the HTMLElement interface is defined as firing a click event on the element. [UIEVENTS]

Firing a synthetic pointer event named e at target, with an optional not trusted flag, means running these steps:

  1. Let event be the result of creating an event using PointerEvent.

  2. Initialize event's type attribute to e.

  3. Initialize event's bubbles and cancelable attributes to true.

  4. Set event's composed flag.

  5. If the not trusted flag is set, initialize event's isTrusted attribute to false.

  6. Initialize event's ctrlKey, shiftKey, altKey, and metaKey attributes according to the current state of the key input device, if any (false for any keys that are not available).

  7. Initialize event's view attribute to target's node document's Window object, if any, and null otherwise.

  8. event's getModifierState() method is to return values appropriately describing the current state of the key input device.

  9. Return the result of dispatching event at target.

Firing a click event at target means firing a synthetic pointer event named click at target.

8.2 The WindowOrWorkerGlobalScope mixin

The WindowOrWorkerGlobalScope mixin is for use of APIs that are to be exposed on Window and WorkerGlobalScope objects.

Other standards are encouraged to further extend it using partial interface mixin WindowOrWorkerGlobalScope { … }; along with an appropriate reference.

typedef (DOMString or Function) TimerHandler;

interface mixin WindowOrWorkerGlobalScope {
  [Replaceable] readonly attribute USVString origin;
  readonly attribute boolean isSecureContext;
  readonly attribute boolean crossOriginIsolated;

  undefined reportError(any e);

  // base64 utility methods
  DOMString btoa(DOMString data);
  ByteString atob(DOMString data);

  // timers
  long setTimeout(TimerHandler handler, optional long timeout = 0, any... arguments);
  undefined clearTimeout(optional long id = 0);
  long setInterval(TimerHandler handler, optional long timeout = 0, any... arguments);
  undefined clearInterval(optional long id = 0);

  // microtask queuing
  undefined queueMicrotask(VoidFunction callback);

  // ImageBitmap
  Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, optional ImageBitmapOptions options = {});
  Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, long sx, long sy, long sw, long sh, optional ImageBitmapOptions options = {});

  // structured cloning
  any structuredClone(any value, optional StructuredSerializeOptions options = {});
};
Window includes WindowOrWorkerGlobalScope;
WorkerGlobalScope includes WindowOrWorkerGlobalScope;
self.isSecureContext

isSecureContext

Support in all current engines.

Firefox49+Safari11.1+Chrome47+
Opera34+Edge79+
Edge (Legacy)15+Internet ExplorerNo
Firefox Android49+Safari iOS11.3+Chrome Android47+WebView Android47+Samsung Internet5.0+Opera Android34+

Returns whether or not this global object represents a secure context. [SECURE-CONTEXTS]

self.origin

origin

Support in all current engines.

Firefox54+Safari11+Chrome59+
Opera46+Edge79+
Edge (Legacy)18Internet ExplorerNo
Firefox Android54+Safari iOS11+Chrome Android59+WebView Android59+Samsung Internet7.0+Opera Android43+

Returns the global object's origin, serialized as string.

self.crossOriginIsolated

crossOriginIsolated

Firefox72+SafariNoChrome87+
Opera73+Edge87+
Edge (Legacy)NoInternet ExplorerNo
Firefox AndroidNoSafari iOSNoChrome Android87+WebView AndroidNoSamsung Internet14.0+Opera AndroidNo

Returns whether scripts running in this global are allowed to use APIs that require cross-origin isolation. This depends on the `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy` HTTP response headers and the "cross-origin-isolated" feature.

Developers are strongly encouraged to use self.origin over location.origin. The former returns the origin of the environment, the latter of the URL of the environment. Imagine the following script executing in a document on https://stargate.example/:

var frame = document.createElement("iframe")
frame.onload = function() {
  var frameWin = frame.contentWindow
  console.log(frameWin.location.origin) // "null"
  console.log(frameWin.origin) // "https://stargate.example"
}
document.body.appendChild(frame)

self.origin is a more reliable security indicator.

The isSecureContext getter steps are to return true if this's relevant settings object is a secure context, or false otherwise.

The origin getter steps are to return this's relevant settings object's origin, serialized.

The crossOriginIsolated getter steps are to return this's relevant settings object's cross-origin isolated capability.

8.3 Base64 utility methods

The atob() and btoa() methods allow developers to transform content to and from the base64 encoding.

In these APIs, for mnemonic purposes, the "b" can be considered to stand for "binary", and the "a" for "ASCII". In practice, though, for primarily historical reasons, both the input and output of these functions are Unicode strings.

result = self.btoa(data)

btoa

Support in all current engines.

Firefox1+Safari3+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer10+
Firefox Android4+Safari iOS1+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android11+

Takes the input data, in the form of a Unicode string containing only characters in the range U+0000 to U+00FF, each representing a binary byte with values 0x00 to 0xFF respectively, and converts it to its base64 representation, which it returns.

Throws an "InvalidCharacterError" DOMException exception if the input string contains any out-of-range characters.

result = self.atob(data)

atob

Support in all current engines.

Firefox1+Safari3+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer10+
Firefox Android4+Safari iOS1+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android11+

Takes the input data, in the form of a Unicode string containing base64-encoded binary data, decodes it, and returns a string consisting of characters in the range U+0000 to U+00FF, each representing a binary byte with values 0x00 to 0xFF respectively, corresponding to that binary data.

Throws an "InvalidCharacterError" DOMException if the input string is not valid base64 data.

The btoa(data) method must throw an "InvalidCharacterError" DOMException if data contains any character whose code point is greater than U+00FF. Otherwise, the user agent must convert data to a byte sequence whose nth byte is the eight-bit representation of the nth code point of data, and then must apply forgiving-base64 encode to that byte sequence and return the result.

The atob(data) method steps are:

  1. Let decodedData be the result of running forgiving-base64 decode on data.

  2. If decodedData is failure, then throw an "InvalidCharacterError" DOMException.

  3. Return decodedData.