👀 Live Preview
Visit count updates on every pageshow (including Back navigation):
Loading…

The onpageshow attribute is an inline event handler for the pageshow event on the window. It runs when the page becomes visible — on first load and when the user returns via the Back button from the back-forward cache (bfcache). Use it to restore drafts saved in onpagehide, refresh stale data, or reinitialize UI. Check event.persisted in script to know if the page was cached. Put onpageshow on <body> or use window.addEventListener("pageshow", …).
Inline JS.
Page visible.
Global scope.
bfcache restore.
Save / restore.
Back button too.
onpageshow AttributeThe primary purpose of onpageshow is to run setup or refresh logic whenever the user sees the page — including when they navigate back and the browser restores a cached copy instead of reloading from the server. Common tasks include restoring form drafts, refetching API data, restarting timers, or updating a “last visited” timestamp.
The pageshow event fires on the window object. Unlike load, which runs only on a full document load, pageshow also runs when a bfcache entry is restored — so your app can fix stale UI that was frozen in memory.
Save work in onpagehide and restore or refresh in onpageshow. Together they cover the full page lifecycle.
Set onpageshow on body or assign window.onpageshow in JavaScript:
<body onpagehide="handlePageHide()" onpageshow="handlePageShow()">
…
</body>
<script>
window.onpageshow = handlePageShow;
window.onpagehide = handlePageHide;
</script>pageshow event fires on the window.<body> as a window event content attribute.window.onpageshow or document.body.onpageshow in script.onpagehide for save-on-leave / restore-on-return.addEventListener to access event.persisted.onload.The onpageshow attribute accepts a string of JavaScript code:
onpageshow="handlePageShow()" — Call a named function.onpageshow="restoreDraft()" — Restore saved form data.window.onpageshow = (event) => { … } — property assignment.function handlePageShow(event) {
restoreDraftFromStorage();
if (event.persisted) {
status.textContent = "Restored from bfcache — refreshing data…";
refetchLatestData();
} else {
status.textContent = "Fresh page load.";
}
}
window.addEventListener("pageshow", handlePageShow);| Property / event | When it fires | Notes |
|---|---|---|
pageshow | Page becomes visible | onpageshow |
pagehide | Page is hidden | onpagehide |
event.persisted | On pageshow / pagehide | true = bfcache |
load | Initial document load | Does not fire on Back |
| Handler attribute | onpageshow 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 |
DOMContentLoaded | Related | DOM ready, not bfcache restore |
onpageshow vs onpagehide vs onload| API | When it fires | Typical use |
|---|---|---|
onpageshow | Page shown (incl. Back) | Restore UI, refresh data |
onpagehide | Page hidden | Save draft, cleanup |
onload / body onload | First full load only | Initial setup |
visibilitychange | Tab hidden / shown | Pause video, analytics |
Inline body handler, dynamic assignment, and bfcache restore with event.persisted.
Visit count updates on every pageshow (including Back navigation):
Loading…
Show visible feedback when the page is shown:
<body onpageshow="onPageShowHandler()">
<p id="status"></p>
</body>
<script>
function onPageShowHandler() {
document.getElementById("status").textContent =
"Page is being shown!";
}
</script>The browser fires pageshow on the window when the page becomes visible. The body attribute runs onPageShowHandler() at that moment.
Set the handler on document.body or window:
<script>
document.body.onpageshow = function (event) {
document.getElementById("log").textContent =
"Page shown at " + new Date().toLocaleTimeString();
};
</script>Assigning document.body.onpageshow is equivalent to the inline body attribute for the pageshow event.
Refresh stale data when the page is restored from cache:
window.addEventListener("pageshow", function (event) {
if (event.persisted) {
log.textContent = "Restored from bfcache — refetching data…";
refetchLatestData();
} else {
log.textContent = "Fresh load — initializing page.";
}
});When event.persisted is true, the browser did not reload the page — refresh any data that may have changed while the user was away.
aria-live="polite" if data reloads on pageshow.Link, Back away.
Back or reload.
onpageshow runs.
Refresh, init.
The pageshow event and onpageshow handler are supported in all modern browsers, including Internet Explorer 11+.
onpageshow supportAll major browsers fire pageshow when the page becomes visible.
Bottom line: Reliable for restore-on-return logic in all modern browsers.
onpageshow with onpagehideevent.persisted and refresh stale dataaddEventListener("pageshow", …) in productionalert() on every pageshowonload alone for Back navigationonpageshow on random divs — use windowThe onpageshow attribute runs JavaScript when the page becomes visible — on first load and when restored from bfcache — essential for refreshing stale UI and restoring user work.
Pair it with onpagehide, read event.persisted with addEventListener, and do not rely on onload alone when users navigate with Back.
onpageshowBookmark these before wiring page restore logic.
Global.
ScopePair both.
Patternbfcache.
APIOn return.
UXBack too.
Gotchapageshow event fires — when the page becomes visible, including first load and bfcache restore.onload fires once on initial load. onpageshow also fires when the user navigates back to a cached page.true when the page was restored from the back-forward cache — JavaScript variables may still hold old values.pageshow fires again — often with event.persisted === true.window.addEventListener("pageshow", handler) is preferred — you need it to read event.persisted.Practice the onpageshow attribute with bfcache and restore examples in the Try It editor.
5 people found this page helpful