4

Scenario:

New web pages are opened, and randomly given hashes.

var urlNoHash = location.href;
history.replaceState(null, null, urlNoHash + "#" + Math.random() );

window.addEventListener("hashchange", myFunction());

function myFunction() {
    alert("hashchange");
}

hashchange is fired when adding new hashes, but not when navigating back with the browser's "Back Button".

1 Answers1

1

I consider this a browser implementation bug: once you've modified history with replaceState, back button will only trigger popstate event, but not hashchange event.

The good news is that changing location.hash also triggers popstate event, so you don't have to listen to both events.

Nash Bridges
  • 2,350
  • 14
  • 19