Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Window: pagehide event

BaselineWidely available

Thepagehide event is sent to aWindow 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 apagehide event before the previous page is shown.

Syntax

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

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

Event type

Event properties

PageTransitionEvent.persistedRead only

Indicates if the document is loading from a cache.

Event handler aliases

In addition to theWindow interface, the event handler propertyonpagehide is also available on the following targets:

Usage notes

Like theunloadandbeforeunload events, this event is not reliably fired by browsers, especially on mobile. For example, thepagehide 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 theunload andbeforeunload events, this event is compatible with theback/forward cache (bfcache), so adding a listener to this event will not prevent the pagefrom being included in the bfcache.

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

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

See thePage 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 forpagehide 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 */    }  },  false,);

This can also be written using theonpagehide event handler property on theWindow:

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

See also

  • Thepageshow 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.

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp