Skip to content

Concept in Capabilities

User activation and the APIs it gates

Published Updated

User activation is a window state that restricts APIs such as popups and vibration to user interactions. The HTML Standard allows these APIs “only when the user is actively interacting with the web page or has interacted with the page at least once”, preventing scripts from invoking them unprompted. The standard defines sticky activation, transient activation, and history-action activation; transient activation can expire after a user-agent-defined duration.

The HTML Standard tracks recent activation with a last activation timestamp and historical sticky activation with a separate boolean.

Each Window carries a last activation timestamp, which is “either a DOMHighResTimeStamp, positive infinity (indicating that W has never been activated), or negative infinity (indicating that the activation has been consumed)”, initially positive infinity. It also carries separate has sticky activation and has history-action activation booleans, both initially false. The user agent also defines a transient activation duration, “a constant number indicating how long a user activation is available for certain user activation-gated APIs”, which the specification expects to be at most a few seconds “so that the user can possibly perceive the link between an interaction with the page and the page calling the activation-gated API” (HTML Standard: Tracking user activation, html.spec.whatwg.org).

Sticky activation is the window’s has sticky activation boolean, which “starts false, then changes to true (and never changes back to false) when W gets the very first activation notification”. Transient activation holds when the current high resolution time is greater than or equal to the last activation timestamp and less than that timestamp plus the transient activation duration, and the state is “considered expired if it becomes false because the transient activation duration time has elapsed since the last user activation”. It can also become false before the expiry time through an activation consumption.

An activation triggering input event is any event whose isTrusted attribute is true and whose type is one of: keydown, provided the key is neither the Esc key nor a shortcut key reserved by the user agent; mousedown; pointerdown, provided the event’s pointerType is mouse; pointerup, provided the event’s pointerType is not mouse; or touchend. MDN’s catalogue adds that the behaviour of excluded keys “may vary by browser” and names Caps Lock, Num Lock and Print Screen as keys that do not produce user activation on some keyboards (User activation, developer.mozilla.org).

The notification and the consumption cover different sets of browsing contexts, as the specification explains: “an activation consumption changes (to false) the transient activation states for all browsing contexts in the page, but an activation notification changes (to true) the states for a subset of those browsing contexts”, because the exhaustive consumption “prevents malicious sites from making multiple calls to an activation consuming API from a single user activation (possibly by exploiting a deep hierarchy of iframes)”.

The specification classifies activation-dependent APIs into four levels. Sticky activation-gated APIs require sticky activation, “so they are blocked until the very first user activation”. Transient activation-gated APIs require transient activation but do not consume it, “so multiple calls are allowed per user activation until the transient state expires”. Transient activation-consuming APIs require transient activation and consume it on each call. History-action activation-consuming APIs require the history-action activation state and consume it on each call, and unlike transient activation “there is no time limit within which such APIs must be used”.

Browser support for the UserActivation interface

Section titled “Browser support for the UserActivation interface”

BCD api.UserActivation records support from Chrome 72, Firefox 120 and Safari 16.4, mirrored into the Chromium-based browsers the record lists (Edge, Opera, Samsung Internet and WebView) and into Chrome Android, Firefox Android and Safari on iOS (api/UserActivation.json, github.com). The same versions apply to both members, hasBeenActive and isActive.

MDN marks both of its lists as not exhaustive. The APIs it lists as requiring transient activation include Navigator.share(), Window.open(), PaymentRequest.show(), Element.requestFullscreen(), MediaDevices.getDisplayMedia(), Document.requestStorageAccess(), Clipboard.read(), Clipboard.write(), USB.requestDevice(), Serial.requestPort(), HID.requestDevice(), Window.showOpenFilePicker(), Window.showSaveFilePicker(), Window.showDirectoryPicker(), EyeDropper.open(), Clients.openWindow() and WindowClient.focus(). The APIs it lists as requiring sticky activation include the beforeunload event, Navigator.vibrate(), VirtualKeyboard.show(), autoplay of the Media and Web Audio APIs (in particular for AudioContexts), and clipboardchange events, which MDN notes can also be enabled by the user granting the clipboard-read permission.

A transient-gated call can run while transient activation remains active, until its duration elapses or a consuming API consumes it for all browsing contexts in the page.

Read sticky and transient activation through navigator.userActivation when the interface is available.

navigator.userActivation exposes hasBeenActive, which “indicates whether the window has sticky user activation”, and isActive, which “indicates whether the window has transient user activation” (UserActivation, developer.mozilla.org). Because the interface arrived at different versions in each engine, the read needs a detection branch.

function activationState() {
if (!('userActivation' in navigator)) {
// Interface absent: branch on the event that fired instead of the window state.
return { hasBeenActive: null, isActive: null, readable: false };
}
const { hasBeenActive, isActive } = navigator.userActivation;
return { hasBeenActive, isActive, readable: true };
}

If the result is readable: false, call the gated API while activation is expected to remain available and handle rejection if activation is absent. The click event does not itself trigger activation; the activation triggering input events are keydown, mousedown, the conditional pointerdown/pointerup pair and touchend. A trusted click normally follows one of them, which is why its handler is a reasonable place to make the call, but the call still needs a rejection handler.

Recovering when a picker or fullscreen request is blocked

Section titled “Recovering when a picker or fullscreen request is blocked”

The HTML Standard requires HTMLInputElement.showPicker() to throw NotAllowedError without transient activation. The Fullscreen Standard accepts transient activation or a user-generated orientation change for requestFullscreen(), consumes activation when the request passes its checks, and rejects the returned promise with TypeError when the fullscreen conditions fail (HTMLInputElement.showPicker(), html.spec.whatwg.org; Element.requestFullscreen(), fullscreen.spec.whatwg.org).

Keep both calls inside their button handlers. This example reports the documented exception names and leaves the native date input and content section available when the programmatic request fails.

<label>Departure <input id="departure" type="date"></label>
<button id="pick" type="button">Choose a date</button>
<section id="preview"><h4>Preview</h4><p>This content also works inline.</p></section>
<button id="fullscreen" type="button">Enter fullscreen</button>
<output id="status" aria-live="polite"></output>
<script>
const departure = document.querySelector('#departure');
const preview = document.querySelector('#preview');
const status = document.querySelector('#status');
document.querySelector('#pick').addEventListener('click', () => {
if (!('showPicker' in HTMLInputElement.prototype)) {
status.value = 'Use the date input directly.';
departure.focus();
return;
}
try {
departure.showPicker();
} catch (error) {
status.value = `${error.name}: use the date input directly.`;
departure.focus();
}
});
document.querySelector('#fullscreen').addEventListener('click', async () => {
if (!preview.requestFullscreen) {
status.value = 'Fullscreen is unavailable; use the inline content.';
return;
}
try {
await preview.requestFullscreen();
} catch (error) {
status.value = `${error.name}: use the inline content.`;
}
});
</script>

If showPicker() throws NotAllowedError, confirm that the call still occurs in the input event handler. Moving it into a timer or calling it after awaited work can leave the call without transient activation because the user-agent-defined duration can expire.

Calling a consuming API while activation is available

Section titled “Calling a consuming API while activation is available”

Web Share is one of the transient activation-consuming APIs, so share() must be called while transient activation is still present. An asynchronous step is a problem only if activation expires or another API consumes it before the call.

shareButton.addEventListener('click', async () => {
if (!('share' in navigator)) {
showCopyLinkDialog();
return;
}
const payload = { title: document.title, url: location.href };
try {
await navigator.share(payload);
} catch (error) {
// NotAllowedError when the activation was missing or already consumed; a
// dismissed share sheet rejects too. Neither is a bug to rethrow.
if (error.name === 'NotAllowedError') showCopyLinkDialog();
}
});

Building payload from values already in hand calls share() promptly after the click, and the catch is the rejection path that covers the activation being gone anyway. An awaited operation can still return while activation is active, but the transient duration can elapse during it; another consuming API can also consume the activation first.

Specifications

SpecificationStatus
None.