The onpagehide attribute is an event handler on <body> (and frameset) for the window pagehide event. It runs when the browser hides the current page while presenting another page from session history — for example, the user presses Back.
The event is a PageTransitionEvent. Check event.persisted to see if the page may stay in the back/forward cache (bfcache). Pair with onpageshow when the page is shown again.
Baseline · Widely available.
Not deprecated. Prefer window.addEventListener("pagehide", …) in scripts; the HTML attribute is the same handler on body.
Not fired on every mobile exit path.
Like unload, some app-switch / force-close cases skip it. Prefer visibilitychange for “session ending”; use pagehide when you care about navigation unload and bfcache.
Syntax
HTML Syntax
syntax.html
<body onpagehide="handler">
Modern equivalent: window.addEventListener("pagehide", 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:
Listener registers on window; leaving the tryit document writes sessionStorage.viaListener.
How It Works
Listen on window — required for addEventListener.
Handler receives the same pagehide event as the attribute.
Does not block bfcache the way many unload listeners can.
Prefer this over inline attributes in larger apps.
Edge Cases & Pitfalls
Most issues come from treating pagehide like a guaranteed “app closed” signal.
mobile
Not every exit fires pagehide
App switch then killing the browser may skip it. Do not rely on it alone for critical analytics.
listen
addEventListener belongs on window
Putting it only on document.body will not receive pagehide.
bfcache
Prefer pagehide over unload for navigation
unload / beforeunload can hurt bfcache. pagehide stays compatible.
work
Keep handlers short
The page is leaving. Persist small data (storage, sendBeacon); avoid long sync work.
pair
Pair with pageshow
Use onpageshow / pageshow to refresh UI when returning from bfcache.
Compatibility
Browser Support
onpagehide is an HTML event attribute for the window pagehide event. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onpagehide
Supported across modern browsers. Delivery can still be skipped on some mobile teardown paths — plan fallbacks when reliability matters.
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
onpagehideWidely supported
Bottom line: Safe to teach and use for navigation hide / bfcache-aware cleanup. Do not treat it as a 100% reliable “app closed” signal on mobile.
Remember
Key Takeaways
Role: run code when the browser hides the page for history navigation.
event.persisted hints at bfcache reuse.
Listen on window with addEventListener.
Not always fired on every mobile close path.
One line:onpagehide runs when the browser hides the page during history navigation — check event.persisted for bfcache.
Frequently Asked Questions
It is an event handler attribute (usually on body) for the window pagehide event. The browser runs your code when it hides the current page while showing another page from session history, such as Back/Forward.
pagehide is a PageTransitionEvent. If event.persisted is true, the page may be kept in the back/forward cache (bfcache) for reuse. If false, the page is more likely being discarded.
No. It is Baseline widely available. Prefer window.addEventListener("pagehide", …) in modern scripts; the body attribute is the HTML form of the same handler.
No. Like unload, it is not reliable on every mobile exit path (for example switching apps then force-closing the browser). For end-of-session signals, visibilitychange is often better; for unload/bfcache-aware cleanup, pagehide is preferred over unload.
No. pagehide is compatible with bfcache. unload/beforeunload listeners are more likely to prevent caching.
The HTML attribute goes on body (or frameset). MDN advises listening on window with addEventListener. addEventListener on body alone will not receive pagehide.