Events
onpageshow
A page is shown after a normal load or history restoration. 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 distinguishes a cached restoration; the page need not be actually visible to the user at this instant. Example: Run this as a standalone page; the browser must generate the relevant lifecycle event. 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 onpageshow="handleEvent(event)"> window.addEventListener("pageshow", event => { /* handle event */ });Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| onpageshow | JavaScript handler body | Optional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute. |
| event | PageTransitionEvent | Event object delivered to the addEventListener callback; event.type is "pageshow". Inspect target, currentTarget, bubbles and cancelable as appropriate. |
Working Example
Example Snippethtml
<p>Window event monitor</p><p role="status" id="log">Waiting for pageshow</p><script> const target = window; const log = document.getElementById("log"); target.addEventListener("pageshow", event => { log.textContent = String(event.type + ": persisted=" + event.persisted); });</script>