onpagehide event
Example
Execute JavaScript when the user leaves the web page:
Definition and Usage
The onpagehide event is triggered when the user leaves the web page.
There are several ways to leave a web page, such as clicking a link, refreshing the page, submitting a form, or closing the browser, etc.
The onpagehide event can sometimes replace onunload the event, but after the onunload event is triggered the page cannot be cached.
To check whether the page is loaded directly from the server or read from the cache, you can use the persisted property of the PageTransitionEvent object. If the page is read from the browser's cache, this property returns true; otherwise it returns false.
Browser support
The numbers in the table indicate the version number of the first browser to support the event.
| Events | |||||
|---|---|---|---|---|---|
| onpagehide | Yes | 11.0 | Yes | 5.0 | Yes |
Syntax
In HTML:
<
element
onpagehide="
myScript
">In JavaScript:
object
.onpagehide=function(){
myScript
};
In JavaScript, use the addEventListener() method:
object
.addEventListener("pagehide",
myScript
);
Note: Internet Explorer 8 and earlier IE versions do not support addEventListener() method.
Technical Details
| Does it support bubbling: | No |
|---|---|
| Is it cancelable: | No |
| Event type: | PageTransitionEvent |
| Supported HTML tags: |
Event Object