Why onpageshow ?
The JavaScript onpageshow property occurs when the page is loaded. But not immediately. onload occurs immediately after the page is loaded. onpageshow takes place after the onload feature (another difference is the time to load in the cache).
<element onpageshow=”script”>
- script: Codes to load when loading a web page.
JavaScript onpageshow Examples
Example 1
<body onpageshow="onPageShowFunction()">
<h2>truecodes.org</h2>
<p>onpageshow experiment</p>
<script>
function onPageShowFunction() {
alert("onpageshow worked successfully")
}
</script>
</body>
Example 2
window.addEventListener("pageshow", onPageShow);
function onPageShow() {
alert("onpageshow worked successfully")
}
Example 3
<body onload="onloadFunction()" onpageshow="onpageFunction()">
<h2>onload VS onpageshow</h2>
<script>
function onloadFunction() {
alert("onloadFunction")
}
function onpageFunction() {
alert("onpageFunction")
}
</script>
</body>
Leave a comment