Events

onpagehide

The page is being hidden as another history entry is shown. Window event handler, exposed through the body element in HTML; register on window rather than on an arbitrary element. Event interface: PageTransitionEvent. For the described native event: bubbles: no; cancelable: no. persisted indicates intended preservation in the back-forward cache; delivery is not guaranteed on mobile. Example: Navigate away from a standalone page. The log is transient and may not remain visible. This example does not request a confirmation or perform cleanup. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.

Formal Syntax & Structure

html syntaxhtml
<body onpagehide="handleEvent(event)">  window.addEventListener("pagehide", event => { /* handle event */ });

Parameters & Attributes

NameTypeDescription
onpagehideJavaScript handler bodyOptional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute.
eventPageTransitionEventEvent object delivered to the addEventListener callback; event.type is "pagehide". Inspect target, currentTarget, bubbles and cancelable as appropriate.

Working Example

Example Snippethtml
<p>Window event monitor</p><p role="status" id="log">Waiting for pagehide</p><script>  const target = window;  const log = document.getElementById("log");  target.addEventListener("pagehide", event => {    log.textContent = String(event.type + ": persisted=" + event.persisted);  });</script>
HTML Reference