Open In App

How to get history changes notification via history.pushState() method ?

Last Updated : 27 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The task is to detect the changes in the browser’s history whenever history.pushState() method is called. To detect the changes in the browser’s history whenever history.pushState() method is called, we would have to monkey-patch (change the behavior of existing function similar to overriding) an object called window.history. We will then insert some of our own logic to execute whenever the browser’s history changes in the function history.pushState.

Syntax:

window.onpopstate = history.onpushstate = function(e) 
    {
      // Code to trigger when the history changes
    };

Below examples illustrate the above approach:

Example 1: Here we are calling the history.pushState from a button click and detecting it. Clicking the modify button we see in the console that the history has been modified and the new state has been added. We also see that the browser URL has been changed and it now points to test.html instead of index.html




<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 id="Geeks" style="color: green;">
         GeeksforGeeks
        </h1>
  
        <!-- Button to modify history of browser -->
        <button id="mod">Modify History</button>
    </center>
    <script src=
    </script>
    <script type="text/javascript">
  
        // Monkey patching window.history
        (function(history) {
            var pushState = history.pushState;
            history.pushState = function(state) {
                if (typeof history.onpushstate == "function")
                {
                    history.onpushstate({
                        state: state
                    });
                }
                return pushState.apply(history, arguments);
            }
        })(window.history);
        window.onpopstate = history.onpushstate = function(e)
        {
            console.log('History has been modified!')
            console.log(e)
        };
        $('#mod').click(function() {
  
            // Adds a gfg state to history and pushes
            // to test.html
            window.history.pushState({
                    gfg: 'Geeks'
                },
                "page 2",
                "test.html");
        });
    </script>
</body>
  
</html>


Output:

History has been modified!
{state: {gfg: "Geeks"}}

Example 2: In this example, we will change the history of the browser as soon as the page loads and detect it. As the page loads, we see in the console that the history has been modified and the new state has been added. We also see that the browser URL has been changed and it now points to test.html instead of index.html




<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 id="Geeks" style="color: green;">
          GeeksforGeeks
        </h1>
    </center>
    <script src=
    </script>
    <script type="text/javascript">
  
        // Monkey patching window.history
        (function(history) {
            var pushState = history.pushState;
            history.pushState = function(state) {
                if (typeof history.onpushstate == "function") 
                {
                    history.onpushstate({
                        state: state
                    });
                }
                return pushState.apply(history, arguments);
            }
        })(window.history);
        window.onpopstate = history.onpushstate = function(e) 
        {
            console.log('History has been modified!')
            console.log(e)
        };
        $(document).ready(function() {
  
            // Adds a gfg state to history and pushes
            // to test.html
            window.history.pushState({
                    gfg: 'Geeks'
                },
                "page 2",
                "test.html");
        });
    </script>
</body>
  
</html>


Output:

History has been modified!
{state: {gfg: "Geeks"}}

Note: Due to security reasons, window.history.pushState might not work in a sandboxed document without a server.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads