Events

onpopstate

A session-history traversal activates a history entry. Window event handler, exposed through the body element in HTML; register on window rather than on an arbitrary element. Event interface: PopStateEvent. For the described native event: bubbles: no; cancelable: no. Read state; calling pushState or replaceState alone does not fire popstate. Initial interaction requirements vary. Example: Navigate between history entries in a standalone page using Back/Forward. 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 onpopstate="handleEvent(event)">  window.addEventListener("popstate", event => { /* handle event */ });

Parameters & Attributes

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

Working Example

Example Snippethtml
<p>Window event monitor</p><p role="status" id="log">Waiting for popstate</p><script>  const target = window;  const log = document.getElementById("log");  target.addEventListener("popstate", event => {    log.textContent = String(JSON.stringify(event.state));  });</script>
HTML Reference