CKTIS / HTML
onpageshow — handling page display
The onpageshow attribute on body assigns JavaScript code to the Window object's pageshow event. It handles both showing a newly loaded document and showing a preserved document again during history navigation.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<body onpageshow="JavaScript code">onpageshow is an event handler content attribute written on the body element. Its value is JavaScript code, not a Boolean value. Despite its location in the markup, it handles the Window object's pageshow event, not an ordinary event on the body element.
For a newly loaded document, pageshow occurs after load. It also occurs when a preserved document is shown again during history navigation. The event parameter is a PageTransitionEvent: event.persisted is false for a newly loaded document and true for a reused document. Using the Back button alone does not guarantee true, because the browser may load the document again.
The attribute on body and the window.onpageshow property refer to the same event handler; do not treat them as two independent function slots. An alternative is window.addEventListener('pageshow', handler), which allows independent handlers to be added. A Content Security Policy can block code in attributes.
The example replaces the paragraph text with information about event.persisted. Opening the file as a new document should produce pageshow: persisted=false. Restoring the same document from the back/forward cache should produce pageshow: persisted=true; restoration is not guaranteed.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>onpageshow example</title>
</head>
<body onpageshow="document.getElementById('status').textContent = 'pageshow: persisted=' + event.persisted">
<p id="status">Waiting for pageshow</p>
</body>
</html>Result
pageshow: persisted=false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.