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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.