HTML onpagehide Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

HTML Syntax

syntax.html
<body onpagehide=" handler">

Modern equivalent: window.addEventListener("pagehide", function (event) { … }); — listen on window, not with addEventListener on body alone.

Attribute Values

The value is a script (event handler code). Useful event details:

DetailMeaningTry it
Handler runsPage is being hidden for history navigationTry it
event.persistedtrue if the page may be kept for bfcache reuseTry it
addEventListenerSame event on window without the HTML attributeTry it

Live Preview

The iframe below uses onpagehide. Click Leave preview page to navigate it away and log the event here.

pagehide demo

Navigating the iframe fires pagehide inside that document (not this tutorial tab).

Log

Waiting for pagehide…

HTML Examples

Short demos. Open Try It Yourself, then follow a link or use the leave button so pagehide can fire.

Example 1 — Save a Note on onpagehide

Write draft text to sessionStorage when the page is hidden.

HTML
<body onpagehide="sessionStorage.setItem('draft', document.getElementById('note').value)">
  <label for="note">Draft</label>
  <textarea id="note" rows="3">Hello</textarea>
  <p><a href="https://example.com">Leave this page</a> to fire pagehide.</p>
</body>
Try It Yourself

How It Works

  1. onpagehide on body registers the window handler.
  2. User navigates away (link, Back, etc.).
  3. Handler runs and stores the draft in sessionStorage.
  4. Later you can restore that value on load or pageshow.

Example 2 — Check event.persisted

See whether the page may enter bfcache when it hides.

HTML
<body onpagehide="
  sessionStorage.setItem(
    'lastHide',
    event.persisted ? 'persisted (bfcache possible)' : 'not persisted'
  );
">
  <p>Leave this page, then come back and read sessionStorage.lastHide.</p>
  <p><a href="https://example.com">Leave</a></p>
</body>
Try It Yourself

How It Works

  1. pagehide provides a PageTransitionEvent.
  2. persisted === true — page may be frozen for Back/Forward reuse.
  3. persisted === false — page is less likely kept in bfcache.
  4. Use this to choose light pause vs heavier teardown logic.

Example 3 — window.addEventListener("pagehide")

Same event without the HTML attribute — preferred in modern scripts.

HTML
<p id="status">Listening for pagehide…</p>
<p><a href="https://example.com">Leave this page</a></p>
<script>
  window.addEventListener("pagehide", function (event) {
    sessionStorage.setItem(
      "viaListener",
      "pagehide (persisted=" + event.persisted + ")"
    );
  });
</script>
Try It Yourself

How It Works

  1. Listen on window — required for addEventListener.
  2. Handler receives the same pagehide event as the attribute.
  3. Does not block bfcache the way many unload listeners can.
  4. 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.

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 Chrome Yes
Full support
Mozilla Firefox Yes
Full support
Apple Safari Yes
Full support
Microsoft Edge Yes
Full support
Opera Yes
Full support
Samsung Internet Yes
Full support
Internet Explorer 11
Supported
onpagehide Widely 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.

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.

Next: onpageshow

Detect when the page is shown again, including restores from bfcache.

onpageshow tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful