onpagehideEvent
Examples
Execute JavaScript when the user leaves the webpage:
Definition and Usage
The onpagehide event is triggered when the user leaves the webpage.
There are multiple 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 theonunloadevent, but the page cannot be cached after the onunload event is triggered.
To determine whether the page is loaded directly from the server or read 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 version number of the first browser that supports this event.
| Event | |||||
|---|---|---|---|---|---|
| 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 IE versions do not support theaddEventListener()method.
Technical Details
| Bubbles: | No |
|---|---|
| Cancelable: | No |
| Event type: | PageTransitionEvent |
| Supported HTML tags: | <body> |
Event Object Other Extensions