The onpageshow attribute is an event handler on <body> (and frameset) for the window pageshow event. It runs when the browser navigates to this document — first load, moving here from another page, Back/Forward (including bfcache restore), and similar cases.
The event is a PageTransitionEvent. On first load it fires afterload. Check event.persisted to detect a bfcache restore. Pair with onpagehide when the page is hidden.
Baseline · Widely available.
Not deprecated. Prefer window.addEventListener("pageshow", …) in scripts; the HTML attribute is the same handler on body.
pageshow ≠ “visible to the user.”
It can fire for a background tab or prerender. For visibility, use visibilitychange (or pagereveal when you need first paint).
Syntax
HTML Syntax
syntax.html
<body onpageshow="handler">
Modern equivalent: window.addEventListener("pageshow", function (event) { … }); — listen on window, not with addEventListener on body alone.
Values
Attribute Values
The value is a script (event handler code). Useful event details:
Detail
Meaning
Try it
Handler runs
Document is being shown via navigation (incl. first load)
persisted tells you if this was a bfcache restore.
Example 2 — Refresh When Restored from bfcache
When event.persisted is true, refresh data that may be stale.
HTML
<body onpageshow="
if (event.persisted) {
document.getElementById('info').textContent =
'Restored from bfcache — refresh timers or data here.';
} else {
document.getElementById('info').textContent =
'Normal show (not from bfcache).';
}
">
<p id="info">Waiting…</p>
<p>Visit another page, then press Back to try a persisted restore.</p>
</body>
bfcache restore: often only pageshow with persisted=true.
Takeaway: put “every time the page is shown” logic in pageshow.
Edge Cases & Pitfalls
Most mistakes confuse pageshow with “visible” or with a one-time load.
visible
Not the same as tab visibility
Background tabs and prerenders can still get pageshow. Use visibilitychange for “user can see it.”
bfcache
load will not re-run from bfcache
Resume timers, WebSockets, or fetch fresh data in pageshow when persisted is true.
listen
addEventListener belongs on window
Listening only on document.body will not receive pageshow.
order
First load: after load
Do not assume pageshow is the first lifecycle event on a cold load.
pair
Pair with pagehide
Pause or persist on pagehide; resume or refresh on pageshow.
Compatibility
Browser Support
onpageshow is an HTML event attribute for the window pageshow event. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onpageshow
Supported across modern browsers. Use it for show / bfcache restore logic; use visibility APIs when you need true on-screen visibility.
97%Global support (approx.)
Google ChromeYes
Full support
Mozilla FirefoxYes
Full support
Apple SafariYes
Full support
Microsoft EdgeYes
Full support
OperaYes
Full support
Samsung InternetYes
Full support
Internet Explorer11
Supported
onpageshowWidely supported
Bottom line: Safe to use for navigation show and bfcache restore. Do not treat it as a guarantee that the tab is foreground-visible.
Remember
Key Takeaways
Role: run code when the browser shows this document.
event.persisted means bfcache restore — refresh stale state.
After load on first navigation; still fires on restore.
Not visibility — background/prerender can still fire it.
One line:onpageshow runs when the document is shown — including bfcache restores that skip load.
Frequently Asked Questions
It is an event handler attribute (usually on body) for the window pageshow event. The browser runs your code when it navigates to the document — first load, history navigation, bfcache restore, and similar cases.
On the first load, pageshow fires after load. Unlike load, pageshow also fires when the page is restored from the back/forward cache (bfcache), where load does not run again.
If event.persisted is true, the page was restored from bfcache (or similarly persisted). Use that to refresh timers, connections, or stale UI.
Not necessarily. It can fire for a background tab or prerender. For actual visibility, prefer visibilitychange (or pagereveal when appropriate).
No. It is Baseline widely available. Prefer window.addEventListener("pageshow", …) in modern scripts; the body attribute is the HTML form of the same handler.
Put the HTML attribute on body (or frameset). For addEventListener, listen on window. addEventListener on body alone will not receive pageshow.