Open In App

HTML DOM onpagehide Event

Last Updated : 15 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The DOM onpagehide event in HTML occurs when the user is getting off a webpage. for example, close the browser window, click on the link, refresh the page, etc. The page to not cached in the onunload event, onpagehide event is 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads