Onpagehide event

Onpagehide event The event object

JavaScript is performed when a user leaves a Web page:

<body onpagehide="myFunction()">



Definitions and usages

The onpagehide event is triggered when the user leaves the page.

There are many ways to leave a Web page. Such as clicking on a link, refreshing the page, submitting the form, closing the browser, etc.

The onpagehide event can sometimes replace the onunload event, but the page cannot be cached after the onunload event is triggered.

To see 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. If the page reads the property from the browser's cache to return the ture, otherwise false is returned.


Browser support

The numbers in the table support the version number of the first browser for the event.

Event
onpagehide Yes 11.0 Yes 5.0 Yes


Grammar

In HTML:

< element onpagehide=" myScript ">

In JavaScript:

object .onpagehide=function(){ myScript };

In JavaScript, use the addEventListener() method:

object .addEventListener("pagehide", myScript );

Note: The addEventListener() method is not supported in Internet Explorer 8 and earlier versions of IE.


Technical details
Whether to support bubbling: No
Can I cancel: No
Types of events: PageTransitionEvent
Supported HTML tags: <body>


Onpagehide event The event object