onpageshow Event

Example

Call a function when a user navigates to a webpage:

<body onpageshow="myFunction()">
Try it Yourself »

More "Try it Yourself" examples below.


Description

The onpageshow event occurs when a user navigates to a webpage.

The onpageshow event is similar to the onload event, except that it occurs after the onload event when the page first loads. Also, the onpageshow event occurs every time the page is loaded, whereas the onload event does not occur when the page is loaded from the cache.

To find out if a page is loaded directly from the server or if the page is cached, you can use the persisted property of the PageTransitionEvent object. This property returns true if the page is cached by the browser, and false otherwise (see "More Examples" below).


Browser Support

The numbers in the table specify the first browser version that fully supports the event.

Event
onpageshow Yes 11.0 Yes 5.0 Yes


Syntax

In HTML:

<element onpageshow="myScript">
Try it Yourself »

In JavaScript:

object.onpageshow = function(){myScript};
Try it Yourself »

In JavaScript, using the addEventListener() method:

object.addEventListener("pageshow", myScript);
Try it Yourself »

Note: The addEventListener() method is not supported in Internet Explorer 8 and earlier versions.


Technical Details

Bubbles: No
Cancelable: No
Event type: PageTransitionEvent
HTML tags: <body>
DOM Version: Level 3 Events

More Examples

Example

Find out whether the page was cached by the browser:

function myFunction(event) {
  alert(event.persisted);
}
Try it Yourself »

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.