onpageshow Event
Definition and Usage
The onpageshow event occurs when the user navigates to the web page.
onpageshow Event and onload EventSimilarly, the difference is that it occurs after the onload event of the page first load. In addition, the onpageshow event occurs each time the page is loaded, and the onload event does not occur when the page is loaded from the cache.
To determine whether the page is loaded directly from the server or from the cache, you can use the PageTransitionEvent object's persisted Property. If the page is cached by the browser, this property returns true, otherwise it returns false (see more examples below).
Example
Example 1
Execute JavaScript when the user navigates to the web page:
<body onpageshow="myFunction()">
Example 2
Check if the page is cached by the browser:
function myFunction(event) { alert(event.persisted); }
Syntax
In HTML:
<element onpageshow="myScript">
In JavaScript:
object.onpageshow = function(){myScript};
In JavaScript, use the addEventListener() method:
object.addEventListener("pageshow", myScript);
Note:Internet Explorer 8 or earlier versions do not support addEventListener() Method.
Technical Details
Bubbling: | Not Supported |
---|---|
Cancelable: | Not Supported |
Event Type: | PageTransitionEvent |
Supported HTML Tags: | <body> |
DOM Version: | Level 3 Events |
Browser Support
The numbers in the table indicate the first browser version that fully supports this event.
Event | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
onpageshow | Support | 11.0 | Support | 5.0 | Support |