onpageswap
An outgoing document is about to be replaced by a cross-document navigation. Window event handler, exposed through the body element in HTML; register on window rather than on an arbitrary element. Event interface: PageSwapEvent. For the described native event: bubbles: no; cancelable: no. Inspect viewTransition and activation when available; do not treat it as an always-delivered cleanup hook. 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
<body onpageswap="handleEvent(event)"> window.addEventListener("pageswap", event => { /* handle event */ });Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| onpageswap | JavaScript handler body | Optional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute. |
| event | PageSwapEvent | Event object delivered to the addEventListener callback; event.type is "pageswap". Inspect target, currentTarget, bubbles and cancelable as appropriate. |
Working Example
<p>Window event monitor</p><p role="status" id="log">Waiting for pageswap</p><script> const target = window; const log = document.getElementById("log"); target.addEventListener("pageswap", event => { log.textContent = String(event.type); });</script>