Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onpageswap — handling an outgoing document during navigation

The onpageswap attribute on body sets the Window handler for the pageswap event. This event concerns the outgoing document during navigation; it is not a notification that a new page has appeared.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<body onpageswap="JavaScript statements">

onpageswap is a content attribute containing JavaScript code for a pageswap event handler. On the body element, it configures the handler on Window, not an independent handler on that element. It is not a Boolean attribute: its value is code, where the event parameter provides the event.

The browser can dispatch pageswap to the outgoing window as navigation deactivates its document. Unloading is not required: the event can also fire when the browser preserves the document in the back/forward cache (bfcache). The real event uses the PageSwapEvent interface; its viewTransition property may provide a view transition or be null. Do not confuse this event with pagereveal, which concerns a document beginning to render. pageswap is not a mechanism for canceling navigation.

The attribute on body and the window.onpageswap property configure the same handler: assigning a new function replaces the previous one. The alternative window.addEventListener('pageswap', handler) adds independent listeners without replacing that property. Content Security Policy can block code in attributes; use an allowed external script in that case.

The example script only logs the results of two checks: whether body has the onpageswap attribute and whether window exposes the onpageswap property. The script does not dispatch an event or initiate navigation, so the output does not verify that the handler defined in the attribute runs.

Examples

<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Body onpageswap handler</title></head>
<body onpageswap="document.body.dataset.received = event.type">
  <script>
    console.log('onpageswap attribute present: ' + document.body.hasAttribute('onpageswap'));
    console.log('onpageswap event property available: ' + ('onpageswap' in window));
  </script>
</body>
</html>

Result

onpageswap attribute present: true
onpageswap event property available: true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources