onpageshow

The onpageshow event in HTML is triggered when a webpage is displayed, allowing developers to perform actions or restore the page's state as the user returns.

onpageshow event

The onpageshow event triggers a script when the page becomes visible after being loaded or navigated to. It is commonly used to manage cached pages or refresh content.

Syntax

In HTML

index.html
<element onpageshow="myScript">

In JS

script.js
object.onpageshow = function(){myScript};

In JavaScript, the addEventListener() function is used.

script.js
object.addEventListener("pageshow", myScript);

Example

::event-onpageshow ::
index.html
<!DOCTYPE html>
<html>
<body onpageshow="myFunction()">

<h1>Hello World!</h1>

<script>
function myFunction() {
  alert("Welcome!");
}
</script>

</body>
</html>

Values

Conclusion

The onpageshow event is helpful for executing actions when a page is displayed or revisited. It is often used for managing cached content or refreshing the page state. This event ensures a smooth user experience by handling actions when the page becomes visible again.