javascriptgoogle-chromebrowser-historyhtml5-history

changing window.location.hash creates entry in history but doesn't affect chrome back button


Update :- As Akansh Gulati Pointed out in his answer if user have any interaction with the page before clicking back button this will work as expected and if user do not have any interaction with the page and press back button then any entry in history ( either by hash-change or by history.push/replace ) will be ignored which is part of Google Chrome update will stop websites hijacking your browser back button

This is valid and logical answer so I am accepting his answer


I am trying to show a success popup after page load and if user press android back button ( which is in this case equivalent to browser back button ) I only want to close the popup ( don't want to redirect back on payment page )

I am adding hash in url when popup is open but when user press back button chrome ignore the hash and redirect back on previous page instead of just removing the hash ( working fine in Firefox )

I have a working example here use following HTML code to reproduce

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
  </head>
  <body onload="test()">
    <button type="button" onclick="window.history.back();">Back</button>
  </body>
  <script type="text/javascript">
    function writeLength() {
      document.body.appendChild(document.createTextNode(window.history.length));
    }

    function test() {
      window.location.hash = 'a';
      setTimeout(function() {
        writeLength();
        window.location.hash = 'b';
        setTimeout(function() {
          writeLength();
          window.location.hash = 'c';
          setTimeout(function() {
            writeLength();
          }, 1500);
        }, 1500);
      }, 1500);
    }
  </script>
</html>

a) open this page in chrome

b) wait till hash is changed to '#c'

c) then press browser back button

expected behavior is it should change the hash back to '#b' and then back to '#a' but it ignore all the hash changes and redirect back to new-tab page

This is the code

      window.location.hash = 'a';
      setTimeout(function() {
        writeLength();
        window.location.hash = 'b';
        setTimeout(function() {
          writeLength();
          window.location.hash = 'c';
          setTimeout(function() {
            writeLength();
          }, 1500);
        }, 1500);
      }, 1500);

how can I simulate the correct behavior (if there is any way) ?

I am using chrome Version 77.0.3865.90 (Official Build) (64-bit) on Mac

here is a GIF image of behavior

actual behavior GIF image


Solution

  • I tried using pushState for further location changes and the problem seems solved. No need to replaceState. The condition is only that user must have clicked/interacted atleast once with screen.

    function test() {
            window.location.hash = 'a';
            setTimeout(function () {
                writeLength();
                window.history.pushState(null, null, `${window.location.pathname}#b`);
                setTimeout(function () {
                    writeLength();
                    window.history.pushState(null, null, `${window.location.pathname}#c`);
                    setTimeout(function () {
                        writeLength();
                    }, 1500);
                }, 1500);
            }, 1500);
        }