Open In App

HTML | DOM PopStateEvent

Last Updated : 20 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The HTML DOM PopStateEvent is an event handler for the popstate event on the window. A popstate event is dispatched to the window every time the active history entry changes between two history entries for the same document.

Property:

  • state: This property/method returns an object containing a copy of the history entries
  • Event Types:

  • popstate: This event occurs when the window’s history changes
  • Example:




    <!DOCTYPE html>
    <html>
      
    <body>
        <script>
            window.onpopstate = function(event) {
               alert("location: " + document.location +
               ", state: " + JSON.stringify(event.state));
            };
            history.pushState({
                page: 1
            }, "title 1", "?page=1");
            history.pushState({
                page: 2
            }, "title 2", "?page=2");
            history.replaceState({
                page: 3
            }, "title 3", "?page=3");
      
            // alerts "location: 
            // state: {"page":1}"
            history.back();
      
            // alerts "location: about:blank, state: null"
            history.back();
        </script>
    </body>
      
    </html>

    
    

    Output:

    Supported Browsers:

    • Google Chrome
    • Mozilla Firefox
    • Opera
    • Edge
    • Safari

    Like Article
    Suggest improvement
    Share your thoughts in the comments

    Similar Reads