HTML onpageshow Attribute

Beginner
⏱️ 7 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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", …).

What You’ll Learn

01

Event handler

Inline JS.

02

pageshow

Page visible.

03

window / body

Global scope.

04

event.persisted

bfcache restore.

05

+ onpagehide

Save / restore.

06

vs onload

Back button too.

Purpose of onpageshow Attribute

The 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.

💡
Pair with onpagehide

Save work in onpagehide and restore or refresh in onpageshow. Together they cover the full page lifecycle.

📝 Syntax

Set onpageshow on body or assign window.onpageshow in JavaScript:

onpageshow.html
<body onpagehide="handlePageHide()" onpageshow="handlePageShow()"></body>

<script>
  window.onpageshow = handlePageShow;
  window.onpagehide = handlePageHide;
</script>

Syntax Rules

  • Value is JavaScript executed when the pageshow event fires on the window.
  • Valid on <body> as a window event content attribute.
  • Also assignable as window.onpageshow or document.body.onpageshow in script.
  • Pair with onpagehide for save-on-leave / restore-on-return.
  • Use addEventListener to access event.persisted.
  • Fires on initial load and bfcache restore — not the same as onload.
  • Not for individual elements — this is a browser page lifecycle event.

💎 Values

The onpageshow attribute accepts a string of JavaScript code:

  • onpageshow="handlePageShow()" — Call a named function.
  • onpageshow="restoreDraft()" — Restore saved form data.
  • JavaScript: window.onpageshow = (event) => { … } — property assignment.
pageshow-handler.html
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);

⚡ Quick Reference

Property / eventWhen it firesNotes
pageshowPage becomes visibleonpageshow
pagehidePage is hiddenonpagehide
event.persistedOn pageshow / pagehidetrue = bfcache
loadInitial document loadDoes not fire on Back
Handler attributeonpageshow on bodyWindow event handler

Applicable Elements

TargetSupported?Notes
<body>YesWindow event on body (HTML)
windowYesPrimary target in JavaScript
<div>, <button>NoUse window listeners
<frameset>Yes (legacy)Rare today
DOMContentLoadedRelatedDOM ready, not bfcache restore

onpageshow vs onpagehide vs onload

APIWhen it firesTypical use
onpageshowPage shown (incl. Back)Restore UI, refresh data
onpagehidePage hiddenSave draft, cleanup
onload / body onloadFirst full load onlyInitial setup
visibilitychangeTab hidden / shownPause video, analytics

Examples Gallery

Inline body handler, dynamic assignment, and bfcache restore with event.persisted.

👀 Live Preview

Visit count updates on every pageshow (including Back navigation):

Loading…

Example — onpageshow on body

Show visible feedback when the page is shown:

body-onpageshow.html
<body onpageshow="onPageShowHandler()">
  <p id="status"></p>
</body>

<script>
  function onPageShowHandler() {
    document.getElementById("status").textContent =
      "Page is being shown!";
  }
</script>
Try It Yourself

How It Works

The browser fires pageshow on the window when the page becomes visible. The body attribute runs onPageShowHandler() at that moment.

Dynamic Values with JavaScript

Set the handler on document.body or window:

dynamic-onpageshow.html
<script>
  document.body.onpageshow = function (event) {
    document.getElementById("log").textContent =
      "Page shown at " + new Date().toLocaleTimeString();
  };
</script>
Try It Yourself

How It Works

Assigning document.body.onpageshow is equivalent to the inline body attribute for the pageshow event.

Example — Detect bfcache with event.persisted

Refresh stale data when the page is restored from cache:

pageshow-persisted.html
window.addEventListener("pageshow", function (event) {
  if (event.persisted) {
    log.textContent = "Restored from bfcache — refetching data…";
    refetchLatestData();
  } else {
    log.textContent = "Fresh load — initializing page.";
  }
});
Try It Yourself

How It Works

When event.persisted is true, the browser did not reload the page — refresh any data that may have changed while the user was away.

♿ Accessibility

  • Restore focus thoughtfully — After bfcache restore, ensure focus order still makes sense.
  • Announce refreshed content — Use aria-live="polite" if data reloads on pageshow.
  • Do not surprise users — Avoid auto-playing media on every pageshow without user intent.
  • Preserve form state — Restore drafts so users do not lose work after navigating back.
  • Keep restore logic fast — Slow refetches on Back navigation hurt perceived performance.

🧠 How onpageshow Works

1

User leaves page

Link, Back away.

pagehide
2

User returns

Back or reload.

Navigate
3

pageshow fires

onpageshow runs.

Event
=

Restore UX

Refresh, init.

Browser Support

The pageshow event and onpageshow handler are supported in all modern browsers, including Internet Explorer 11+.

Page Lifecycle · Fully supported

Universal onpageshow support

All major browsers fire pageshow when the page becomes visible.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 11+ supported
Full support
Opera Fully supported
Full support
onpageshow attribute 99% supported

Bottom line: Reliable for restore-on-return logic in all modern browsers.

💡 Best Practices

✅ Do

  • Pair onpageshow with onpagehide
  • Check event.persisted and refresh stale data
  • Use addEventListener("pageshow", …) in production
  • Restore drafts saved before the user left
  • Test with Back button navigation

❌ Don’t

  • Use alert() on every pageshow
  • Assume pageshow equals a fresh server load
  • Rely on onload alone for Back navigation
  • Run heavy sync on every bfcache restore
  • Put onpageshow on random divs — use window

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onpageshow

Bookmark these before wiring page restore logic.

5
Core concepts
🔄 02

+ onpagehide

Pair both.

Pattern
🔎 03

.persisted

bfcache.

API
💾 04

Restore UI

On return.

UX
05

Not onload

Back too.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the pageshow event fires — when the page becomes visible, including first load and bfcache restore.
No. 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.
Load the page, navigate to another URL, then click Back. pageshow fires again — often with event.persisted === true.
window.addEventListener("pageshow", handler) is preferred — you need it to read event.persisted.
Yes in all modern browsers; Internet Explorer 11+.

Handle page restore gracefully

Practice the onpageshow attribute with bfcache and restore examples in the Try It editor.

Try onpageshow demo →

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.

5 people found this page helpful