Call a function when a user navigates to a webpage:
<body onpageshow="myFunction()">
Try it Yourself »
More "Try it Yourself" examples below.
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).
The numbers in the table specify the first browser version that fully supports the event.
Event | |||||
---|---|---|---|---|---|
onpageshow | Yes | 11.0 | Yes | 5.0 | Yes |
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.
Bubbles: | No |
---|---|
Cancelable: | No |
Event type: | PageTransitionEvent |
HTML tags: | <body> |
DOM Version: | Level 3 Events |
Find out whether the page was cached by the browser:
function myFunction(event) {
alert(event.persisted);
}
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!