I would like to listen to path changes in a SPA which is not maintained by me.
I found one solution here: https://stackoverflow.com/a/44819548/7042552
But still, it seems kind of “hacky” to me – but still my implementation is like this:
JavaScript
x
13
13
1
let url = window.location.href;
2
3
['click','popstate', 'onload'].forEach( evt =>
4
window.addEventListener(evt, function () {
5
requestAnimationFrame(()=>{
6
if (url !== location.href) {
7
// do stuff
8
}
9
url = location.href;
10
});
11
}, true)
12
);
13
Is there a better or more generic way to listen for page loads in a SPA?
Advertisement
Answer
This https://stackoverflow.com/a/41825103/7042552 did the job for me, unbelievable we still have to use these hacks in 2018.