onpagehide
Event
Example
Execute JavaScript when the user leaves the webpage:
<body onpagehide="myFunction()">
Definition and Usage
The onpagehide event is triggered when the user leaves the webpage.
There are various ways to leave a webpage, such as clicking a link, refreshing the page, submitting a form, closing the browser, etc.
The onpagehide event can sometimes replace the onunload event, but the onunload event cannot cache the page after it is triggered.
To determine whether the page is loaded directly from the server or from the cache, you can use the persisted property of the PageTransitionEvent object. This property returns true if the page is read from the browser's cache, otherwise it returns false.
Browser Support
The numbers in the table indicate the first browser version that supports the event.
Event | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
onpagehide | Yes | 11.0 | Yes | 5.0 | Yes |
Syntax
In HTML:
In JavaScript:
In JavaScript, using the addEventListener() method:
Note: Internet Explorer 8 and earlier versions do not support the addEventListener() method.
Bubbles: | No |
---|---|
Cancelable: | No |
--- | --- |
Event Type: | PageTransitionEvent |
--- | --- |
Supported HTML Tags: | <body> |
--- | --- |