HTML onpageshow Attribute

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

Definition and Usage

The onpageshow attribute is an event handler on <body> (and frameset) for the window pageshow event. It runs when the browser navigates to this document — first load, moving here from another page, Back/Forward (including bfcache restore), and similar cases.

The event is a PageTransitionEvent. On first load it fires after load. Check event.persisted to detect a bfcache restore. Pair with onpagehide when the page is hidden.

Baseline · Widely available.

Not deprecated. Prefer window.addEventListener("pageshow", …) in scripts; the HTML attribute is the same handler on body.

pageshow ≠ “visible to the user.”

It can fire for a background tab or prerender. For visibility, use visibilitychange (or pagereveal when you need first paint).

HTML Syntax

syntax.html
<body onpageshow=" handler">

Modern equivalent: window.addEventListener("pageshow", 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 runsDocument is being shown via navigation (incl. first load)Try it
event.persistedtrue when restored from bfcacheTry it
After loadOn first load, pageshow fires after loadTry it

Live Preview

The iframe loads a tiny page with onpageshow. It posts the event here (including persisted). Click Reload preview to fire it again.

pageshow demo

A normal reload usually has persisted=false. Back/Forward from bfcache can set persisted=true.

Log

Waiting for pageshow…

HTML Examples

Short demos. Use Try It Yourself for a live editor — reload or navigate Back to explore persisted.

Example 1 — Show a Status on onpageshow

Update the page when the document is shown.

HTML
<body onpageshow="document.getElementById('status').textContent =
  'pageshow fired (persisted=' + event.persisted + ')'">
  <p id="status">Waiting…</p>
</body>
Try It Yourself

How It Works

  1. onpageshow on body registers the window handler.
  2. Browser shows the document (load or restore).
  3. Handler runs and updates the status text.
  4. persisted tells you if this was a bfcache restore.

Example 2 — Refresh When Restored from bfcache

When event.persisted is true, refresh data that may be stale.

HTML
<body onpageshow="
  if (event.persisted) {
    document.getElementById('info').textContent =
      'Restored from bfcache — refresh timers or data here.';
  } else {
    document.getElementById('info').textContent =
      'Normal show (not from bfcache).';
  }
">
  <p id="info">Waiting…</p>
  <p>Visit another page, then press Back to try a persisted restore.</p>
</body>
Try It Yourself

How It Works

  1. pageshow provides a PageTransitionEvent.
  2. persisted === true — page came back from bfcache.
  3. load does not re-run in that case — so use pageshow to wake UI.
  4. persisted === false — typical first navigation/load path.

Example 3 — load Then pageshow

Log order with addEventListener — preferred in modern scripts.

HTML
<pre id="log"></pre>
<script>
  function line(text) {
    document.getElementById("log").textContent += text + "\\n";
  }
  window.addEventListener("load", function () {
    line("1) load");
  });
  window.addEventListener("pageshow", function (event) {
    line("2) pageshow (persisted=" + event.persisted + ")");
  });
</script>
Try It Yourself

How It Works

  1. Listeners register on window.
  2. First load: load runs, then pageshow.
  3. bfcache restore: often only pageshow with persisted=true.
  4. Takeaway: put “every time the page is shown” logic in pageshow.

Edge Cases & Pitfalls

Most mistakes confuse pageshow with “visible” or with a one-time load.

visible

Not the same as tab visibility

Background tabs and prerenders can still get pageshow. Use visibilitychange for “user can see it.”

bfcache

load will not re-run from bfcache

Resume timers, WebSockets, or fetch fresh data in pageshow when persisted is true.

listen

addEventListener belongs on window

Listening only on document.body will not receive pageshow.

order

First load: after load

Do not assume pageshow is the first lifecycle event on a cold load.

pair

Pair with pagehide

Pause or persist on pagehide; resume or refresh on pageshow.

Browser Support

onpageshow is an HTML event attribute for the window pageshow event. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onpageshow

Supported across modern browsers. Use it for show / bfcache restore logic; use visibility APIs when you need true on-screen visibility.

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
onpageshow Widely supported

Bottom line: Safe to use for navigation show and bfcache restore. Do not treat it as a guarantee that the tab is foreground-visible.

Key Takeaways

  • Role: run code when the browser shows this document.
  • event.persisted means bfcache restore — refresh stale state.
  • After load on first navigation; still fires on restore.
  • Not visibility — background/prerender can still fire it.

One line: onpageshow runs when the document is shown — including bfcache restores that skip load.

Frequently Asked Questions

It is an event handler attribute (usually on body) for the window pageshow event. The browser runs your code when it navigates to the document — first load, history navigation, bfcache restore, and similar cases.
On the first load, pageshow fires after load. Unlike load, pageshow also fires when the page is restored from the back/forward cache (bfcache), where load does not run again.
If event.persisted is true, the page was restored from bfcache (or similarly persisted). Use that to refresh timers, connections, or stale UI.
Not necessarily. It can fire for a background tab or prerender. For actual visibility, prefer visibilitychange (or pagereveal when appropriate).
No. It is Baseline widely available. Prefer window.addEventListener("pageshow", …) in modern scripts; the body attribute is the HTML form of the same handler.
Put the HTML attribute on body (or frameset). For addEventListener, listen on window. addEventListener on body alone will not receive pageshow.

Next: onpaste

Handle paste events on form controls and editable content.

onpaste tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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