👀 Live Preview
Navigate away from this page and return with Back — a timestamp is saved on pagehide:
No pagehide save yet — leave this page and come back to test.

The onpagehide attribute is an inline event handler for the pagehide event on the window. It runs when the browser hides the page — for example when the user navigates away, closes the tab, or the page enters the back-forward cache (bfcache). Use it to save drafts, flush analytics, or release resources. The page is not always destroyed; check event.persisted in script. Pair with onpageshow when the page becomes visible again. Put onpagehide on <body> or use window.addEventListener("pagehide", …).
Inline JS.
Page hidden.
Global scope.
bfcache hint.
Restore UX.
sessionStorage.
onpagehide AttributeThe primary purpose of onpagehide is to run cleanup or save work when the user leaves the page — without blocking navigation the way onbeforeunload can. Common tasks include saving form drafts to sessionStorage, sending beacon analytics, closing WebSocket connections, or pausing timers.
The pagehide event fires on the window object. Unlike the legacy unload event, pagehide works well with the back-forward cache: the page may stay in memory and return instantly when the user clicks Back.
When event.persisted is true, the page was cached — do not assume all JavaScript state was reset. Use onpageshow to refresh stale data when the user returns.
Set onpagehide on body or assign window.onpagehide in JavaScript:
<body onpagehide="handlePageHide()" onpageshow="handlePageShow()">
…
</body>
<script>
window.onpagehide = handlePageHide;
window.onpageshow = handlePageShow;
</script>pagehide event fires on the window.<body> as a window event content attribute.window.onpagehide in script.onpageshow when the page becomes visible again.addEventListener to access event.persisted.onbeforeunload (confirm dialog) or unload (legacy).The onpagehide attribute accepts a string of JavaScript code:
onpagehide="saveDraft()" — Call a named function.onpagehide="sessionStorage.setItem('draft', draft.value)" — Inline save.window.onpagehide = (event) => { … } — property assignment.function handlePageHide(event) {
sessionStorage.setItem("draft", draftInput.value);
if (event.persisted) {
status.textContent = "Page cached (bfcache) — draft saved.";
} else {
status.textContent = "Page leaving — draft saved.";
}
}
window.addEventListener("pagehide", handlePageHide);| Property / event | When it fires | Notes |
|---|---|---|
pagehide | Page is hidden | onpagehide |
pageshow | Page becomes visible | onpageshow |
event.persisted | On pagehide / pageshow | true = bfcache |
| Event target | window | Global browser event |
| Handler attribute | onpagehide on body | Window event handler |
| Target | Supported? | Notes |
|---|---|---|
<body> | Yes | Window event on body (HTML) |
window | Yes | Primary target in JavaScript |
<div>, <button> | No | Use window listeners |
<frameset> | Yes (legacy) | Rare today |
visibilitychange | Related | Tab hidden, not full navigation |
onpagehide vs onpageshow vs onbeforeunload| API | When it fires | Typical use |
|---|---|---|
onpagehide | Page hidden | Save draft, cleanup |
onpageshow | Page shown | Restore UI, refresh data |
onbeforeunload | Before leave | Unsaved-changes warning |
unload (legacy) | Document unload | Avoid — breaks bfcache |
Inline body handler, window.onpagehide assignment, and draft save with event.persisted.
Navigate away from this page and return with Back — a timestamp is saved on pagehide:
No pagehide save yet — leave this page and come back to test.
Save a draft when the page is hidden:
<body onpagehide="handlePageHide()">
<textarea id="draft"></textarea>
<p id="status"></p>
</body>
<script>
function handlePageHide() {
sessionStorage.setItem("draft",
document.getElementById("draft").value);
document.getElementById("status").textContent =
"Draft saved on pagehide.";
}
</script>The browser fires pagehide on the window when the page is hidden. The body attribute runs your handler at that moment.
Assign window.onpagehide dynamically:
<script>
window.onpagehide = function () {
document.getElementById("log").textContent =
"Page is being hidden — cleanup ran.";
sessionStorage.setItem("lastHide", Date.now());
};
</script>Property assignment on window is equivalent to the inline body handler for the pagehide event.
Handle cached vs destroyed pages differently:
window.addEventListener("pagehide", function (event) {
saveDraft();
if (event.persisted) {
log.textContent = "Page cached — may return via Back button.";
} else {
log.textContent = "Page discarded — full navigation away.";
}
});event.persisted tells you whether the browser kept the page in memory. Use addEventListener — inline onpagehide cannot read the event object easily.
onbeforeunload sparingly; it interrupts users.onpageshow with polite status text if data was reloaded.Editing, reading.
Link, close tab.
onpagehide runs.
Draft, analytics.
The pagehide event and onpagehide handler are supported in all modern browsers, including Internet Explorer 11+.
onpagehide supportAll major browsers fire pagehide when the page is hidden from view.
Bottom line: Reliable for save-on-leave logic in all modern browsers.
onpagehide with onpageshowsessionStorage or sendBeaconaddEventListener("pagehide", …) for event.persistedalert() on pagehide — the page is leavingunload handlers — they hurt bfcachesendBeacononpagehide on random divs — use windowThe onpagehide attribute runs JavaScript when the browser hides the page — ideal for saving drafts, sending analytics, and cleaning up before navigation.
Pair it with onpageshow, read event.persisted with addEventListener, and avoid legacy unload handlers that break bfcache.
onpagehideBookmark these before wiring page lifecycle handlers.
Global.
ScopePair both.
Patternbfcache.
APIOn leave.
UXPage cached.
Gotchapagehide event fires — when the browser hides the page during navigation or tab close.pagehide is modern and bfcache-friendly. The legacy unload event can prevent back-forward caching — prefer pagehide.true when the page enters the back-forward cache instead of being destroyed — common when using the Back button.sessionStorage to verify the handler ran.window.addEventListener("pagehide", handler) is preferred — you need it to read event.persisted.Practice the onpagehide attribute with draft-saving examples in the Try It editor.
5 people found this page helpful