HTML | DOM onpagehide Event
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 ondrop Event
- HTML | DOM ondurationchange Event
- HTML | DOM onended Event
- HTML | DOM onfocus Event
- HTML | DOM onafterprint Event
- HTML | DOM animationend Event
- HTML | DOM ondragover Event
- HTML | DOM onseeking Event
- HTML | DOM ondragstart Event
- HTML | DOM oncut Event
- HTML | DOM onerror Event
- HTML | DOM fullscreenchange Event
- HTML | DOM animationiteration Event
- HTML | DOM animationstart Event
- HTML | DOM ondragenter 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.



