files/en-us/web/api/window/pageshow_event/index.md
{{APIRef("HTML DOM")}}
The pageshow event is sent to a {{domxref("Window")}} when the browser navigates to a new document.
This includes:
[!WARNING] Despite the name, the
pageshowevent 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:
- {{domxref("window/pagereveal_event", "pagereveal")}}: Sent when a page is first rendered.
- {{domxref("document/visibilitychange_event", "visibilitychange")}}: Sent each time a page's visibility changes.
- {{domxref("document/prerenderingchange_event", "prerenderingchange")}}: Sent when a prerendered page is activated.
[!NOTE] During the initial page load, the
pageshowevent fires after the {{domxref("Window/load_event", "load")}} event.
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
addEventListener("pageshow", (event) => { })
onpageshow = (event) => { }
A {{domxref("PageTransitionEvent")}}. Inherits from {{domxref("Event")}}.
{{InheritanceDiagram("PageTransitionEvent")}}
In addition to the Window interface, the event handler property onpageshow is also available on the following targets:
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 {{domxref("PageTransitionEvent.persisted", "persisted")}} flag on pageshow and pagehide events.
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));
<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>
{{EmbedLiveSample("Examples", 640, 250)}}
{{Specifications}}
{{Compat}}