onpageshowEvent

事件对象参考手册Event Object

Examples

Trigger JavaScript when the user browses a page:

<body onpageshow="myFunction()">

Try it Yourself »

More examples are included at the bottom of this article.


Definition and Usage

The onpageshow event is triggered when the user browses a page.

The onpageshow event is similar to theonloadonload event. The onload event is triggered when the page is loaded for the first time, while the onpageshow event is triggered every time the page is loaded. That is, the onload event is not triggered when the page is read from the browser cache.

To check whether the page is loaded directly from the server or read from the cache, you can use the persisted property of the PageTransitionEvent object to determine this. If the page is read from the browser cache, this property returns true, otherwise it returns false (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:Internet Explorer 8 and earlier IE versions do not support theaddEventListener()method.


Technical Details
Bubbles: No
Cancelable: No
Event type: PageTransitionEvent
Supported HTML tags: <body>


Examples

More Examples

Examples

Check whether the page is read from the browser cache:

function myFunction(event) {
alert("Was this page loaded from browser cache? " + event.persisted);
}

Try it Yourself »


事件对象参考手册Event Object Other Extensions