Call a function when the user is navigating away from a webpage:
<body onpagehide="myFunction()">
The onpagehide event occurs when the user is navigating away from a webpage.
There are several ways to navigate away from a page. E.g. by clicking on a link, refreshing the page, submitting a form, closing the browser window, etc.
The onpagehide event is sometimes used instead of the onunload event, as the onunload event causes the page to not be cached.
To find out if a page is loaded directly from the server or if the page is cached, you can use the persisted property of the PageTransitionEvent object. This property returns true if the page is cached by the browser, and false otherwise.
The numbers in the table specify the first browser version that fully supports the event.
Event | |||||
---|---|---|---|---|---|
onpagehide | Yes | 11.0 | Yes | 5.0 | Yes |
Note: Due to different browser settings, this event may not always work as expected.
In HTML:
<
element onpagehide="
myScript">
In JavaScript:
object.onpagehide = function(){
myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("pagehide",
myScript);
Bubbles: | No |
---|---|
Cancelable: | No |
Event type: | PageTransitionEvent |
HTML tags: | <body> |
DOM Version: | Level 3 Events |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!