onpagehideEvent

事件对象参考手册Event Object

Examples

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 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:

<element onpagehide="myScript">

In JavaScript:

object.onpagehide=function(){myScript};

In JavaScript, using the addEventListener() method:

object.addEventListener("pagehide", myScript);

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