Window: pagehide event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The pagehide event is sent to a Window when the browser hides the current page in the process of presenting a different page from the session's history.

For example, when the user clicks the browser's Back button, the current page receives a pagehide event before the previous page is shown.

Syntax

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

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

onpagehide = (event) => { }

Event type

A PageTransitionEvent. Inherits from Event.

Event PageTransitionEvent

Event properties

PageTransitionEvent.persisted Read only

Indicates if the document is loading from a cache.

Event handler aliases

In addition to the Window interface, the event handler property onpagehide is also available on the following targets:

Usage notes

Like the unload and beforeunload events, this event is not reliably fired by browsers, especially on mobile. For example, the pagehide event is not fired at all in the following scenario:

  1. A mobile user visits your page.
  2. The user then switches to a different app.
  3. Later, the user closes the browser from the app manager.

However, unlike the unload and beforeunload events, this event is compatible with the back/forward cache (bfcache), so adding a listener to this event will not prevent the page from being included in the bfcache.

The best event to use to signal the end of a user's session is the visibilitychange event. In browsers that don't support visibilitychange the pagehide event is the next-best alternative.

If you're specifically trying to detect page unload events, the pagehide event is the best option.

See the Page Lifecycle API guide for more information about how this event relates to other events in the page lifecycle.

Examples

In this example, an event handler is established to watch for pagehide events and to perform special handling if the page is being persisted for possible reuse.

js
window.addEventListener("pagehide", (event) => {
  if (event.persisted) {
    /* the page isn't being discarded, so it can be reused later */
  }
});

This can also be written using the onpagehide event handler property on the Window:

js
window.onpagehide = (event) => {
  if (event.persisted) {
    /* the page isn't being discarded, so it can be reused later */
  }
};

Specifications

Specification
HTML
# event-pagehide

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
pagehide event
Chrome – Full support
Chrome 3 (Release date: 2009-09-15)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 6 (Release date: 2011-08-16)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 6 (Release date: 2011-08-16)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Deno – No support
Deno
footnote No support

Legend

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

Full support
Full support
No support
No support

See also

  • The pageshow event.
  • Page Lifecycle API gives best-practices guidance on handling page lifecycle behavior in your web applications.
  • PageLifecycle.js: a JavaScript library that deals with cross-browser inconsistencies in page lifecycle behavior.
  • Back/forward cache explains what the back/forward cache is, and its implications for various page lifecycle events.