The DOM onpagehide event in HTML occurs when the user is getting off from a webpage. for example, close the browser window, click on the link, refresh the page, etc.
The page to not be cached in onunload event, onpagehide event used instead of the onunload event.
Syntax:
- In HTML:
<element onpagehide="myScript">
- In JavaScript:
object.onpagehide = function(){myScript}; - In JavaScript, using the addEventListener() method:
object.addEventListener("pagehide", myScript);
Example 1: using Javascript
<!DOCTYPE html> <html> <head> <title> DOM onpagehide event </title> </head> <body> <h1 id="hID"></h1> <script> document.getElementsByTagName( "BODY")[0].onpagehide = function() {GFGfun()}; function GFGfun() { document.getElementById( "hID").innerHTML = "Thank you!"; }; </script> </body> </html> |
Example 2: Using the addEventListener() method
<!DOCTYPE html> <html> <head> <title> DOM onpagehide event </title> </head> <body> <h1 id="hID"></h1> <script> window.addEventListener("pagehide", GFGfun); function GFGfun() { document.getElementById( "hID").innerHTML = "Thank You"; } </script> </body> </html> |
Supported Browsers: The browsers supported by DOM onpagehide Event are listed below:
- Google Chrome
- Internet Explorer 11.0
- Firefox
- Apple Safari 5.0
- Opera
Recommended Posts:
- HTML | DOM fullscreenerror Event
- HTML | DOM onvolumechange Event
- HTML | DOM onopen Event
- HTML | DOM ondragleave Event
- HTML | DOM onpaste Event
- HTML | DOM touchcancel Event
- HTML | DOM touchmove Event
- HTML | DOM Storage Event
- HTML | DOM createEvent() Event Method
- HTML | DOM touchend Event
- HTML | DOM timeStamp Event Property
- HTML | DOM stopPropagation() Event Method
- HTML | DOM cancelable Event Property
- HTML | DOM type Event Property
- HTML | DOM touchstart Event
- HTML | DOM onpause Event
- HTML | DOM onabort Event
- HTML | DOM onsuspend Event
- HTML | DOM oninvalid Event
- HTML | DOM onfocus Event
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


