Window: pageshow 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 pageshow event is sent to a Window when the browser navigates to a new document.

This includes:

  • Initially loading the page.
  • Navigating to the page from another page in the same window or tab.
  • Restoring a frozen page on mobile OSes.
  • Returning to the page using the browser's forward or back buttons (including when restored from the bfcache).
  • Opening a page in a background tab.
  • Prerendering a page, even before it is activated.

Warning: Despite the name, the pageshow event does not fire when the page is actually shown to the user. For example, it may be opened in a background tab or prerendered. If you are interested in responding to the page being shown to the user, use the following events:

Note: During the initial page load, the pageshow event fires after the load event.

Syntax

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

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

onpageshow = (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 onpageshow is also available on the following targets:

Examples

This example sets up event handlers for events listed in the array events. The handler, eventLogger(), logs the type of event that occurred to the console, and includes the value of the persisted flag on pageshow and pagehide events.

JavaScript

js
const events = ["pagehide", "pageshow", "unload", "load"];

const eventLogger = (event) => {
  switch (event.type) {
    case "pagehide":
    case "pageshow": {
      let isPersisted = event.persisted ? "persisted" : "not persisted";
      console.log(`Event: ${event.type} - ${isPersisted}`);
      break;
    }
    default:
      console.log(`Event: ${event.type}`);
      break;
  }
};

events.forEach((eventName) => window.addEventListener(eventName, eventLogger));

HTML

html
<p>
  Open the console and watch the output as you navigate to and from this page.
  Try loading new pages into this tab, then navigating forward and backward
  through history, noting the events' output to the log.
</p>

Results

Specifications

Specification
HTML
# event-pageshow

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
pageshow 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