Window: pageswap event

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The pageswap event is fired when you navigate across documents, when the previous document is about to unload.

This is useful in the case of cross-document (MPA) view transitions for manipulating an active transition from the outbound page of a navigation. For example, you might wish to skip the transition, or customize the outbound transition animation via JavaScript.

It also provides access to the navigation type and current and destination document history entries.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("pageswap", (event) => { })

onpageswap = (event) => { }

Event type

A PageSwapEvent. Inherits from Event.

Event PageSwapEvent

Event properties

PageSwapEvent.activation Read only

Returns a NavigationActivation object containing the navigation type and current and destination document history entries for a same-origin navigation. If the navigation has a cross-origin URL anywhere in the redirect chain, it returns null.

PageSwapEvent.viewTransition Read only

Returns the ViewTransition object representing the inbound cross-document view transition, if one is active when the event is fired. If this is not the case, it returns null.

Examples

js
window.addEventListener("pageswap", async (e) => {
  // Only run this if an active view transition exists
  if (e.viewTransition) {
    const currentUrl = e.activation.from?.url
      ? new URL(e.activation.from.url)
      : null;
    const targetUrl = new URL(e.activation.entry.url);

    // Going from profile page to homepage
    // ~> The big img and title are the ones!
    if (isProfilePage(currentUrl) && isHomePage(targetUrl)) {
      // Set view-transition-name values on the elements to animate
      document.querySelector(`#detail main h1`).style.viewTransitionName =
        "name";
      document.querySelector(`#detail main img`).style.viewTransitionName =
        "avatar";

      // Remove view-transition-names after snapshots have been taken
      // Stops naming conflicts resulting from the page state persisting in BFCache
      await e.viewTransition.finished;
      document.querySelector(`#detail main h1`).style.viewTransitionName =
        "none";
      document.querySelector(`#detail main img`).style.viewTransitionName =
        "none";
    }

    // Going to profile page
    // ~> The clicked items are the ones!
    if (isProfilePage(targetUrl)) {
      const profile = extractProfileNameFromUrl(targetUrl);

      // Set view-transition-name values on the elements to animate
      document.querySelector(`#${profile} span`).style.viewTransitionName =
        "name";
      document.querySelector(`#${profile} img`).style.viewTransitionName =
        "avatar";

      // Remove view-transition-names after snapshots have been taken
      // Stops naming conflicts resulting from the page state persisting in BFCache
      await e.viewTransition.finished;
      document.querySelector(`#${profile} span`).style.viewTransitionName =
        "none";
      document.querySelector(`#${profile} img`).style.viewTransitionName =
        "none";
    }
  }
});

Note: See List of Chrome DevRel team members for the live demo this code is taken from.

Specifications

Specification
HTML
# event-pageswap

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
pageswap event
Chrome – Full support
Chrome 124 (Release date: 2024-04-16)
footnote Full support
Edge – Full support
Edge 124 (Release date: 2024-04-18)
footnote Full support
Firefox – No support
Firefox
footnote
footnote See bug 1881438
Opera – Full support
Opera 110 (Release date: 2024-05-14)
footnote Full support
Safari – Partial support
Safari 18.2 (Release date: 2024-12-11)
footnote Partial support
footnote Cross-origin navigation does not fire pageswap. See bug 306447.
Chrome Android – Full support
Chrome Android 124 (Release date: 2024-04-16)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote
footnote See bug 1881438
Opera Android – Full support
Opera Android 82 (Release date: 2024-05-02)
footnote Full support
Safari on iOS – Partial support
Safari on iOS 18.2 (Release date: 2024-12-11)
footnote Partial support
footnote Cross-origin navigation does not fire pageswap. See bug 306447.
Samsung Internet – Full support
Samsung Internet 27 (Release date: 2024-11-06)
footnote Full support
WebView Android – Full support
WebView Android 124 (Release date: 2024-04-16)
footnote Full support
WebView on iOS – Partial support
WebView on iOS 18.2 (Release date: 2024-12-11)
footnote Partial support
footnote Cross-origin navigation does not fire pageswap. See bug 306447.
Deno – No support
Deno
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.

See also