The documentation for the popstate
Window event states:
Note that just calling
history.pushState()
orhistory.replaceState()
won't trigger apopstate
event. Thepopstate
event will be triggered by doing a browser action such as a click on the back or forward button (or callinghistory.back()
orhistory.forward()
in JavaScript).
I need a way to execute some code when the URL changes by means that don't trigger the popstate
event (such as history.replaceState()
). Preferably, using the MutationObserver API[1], and most definitely without polling the URL every x seconds.
Also, hashchange
won't do, as I must act on every change in the URL, not just the hash part.
Is that possible?
[1] It is explained on another question why the MutationObserver API doesn't work for this.
(other) Related questions: