Skip to content
Related Articles
Open in App
Not now

Related Articles

HTML | DOM onpagehide Event

Improve Article
Save Article
  • Last Updated : 16 Aug, 2022
Improve Article
Save Article

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.
Supported Tags

  • <body>

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 
 

html




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

html




<!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 3
  • Edge 12
  • Internet Explorer 11.0
  • Firefox 6
  • Apple Safari 5.0
  • Opera 15

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!