Skip to content

Instantly share code, notes, and snippets.

@saitergun
Last active April 4, 2022 18:04
Show Gist options
  • Save saitergun/6a3627232c2c6072abc0501715d569e9 to your computer and use it in GitHub Desktop.
Save saitergun/6a3627232c2c6072abc0501715d569e9 to your computer and use it in GitHub Desktop.
catch url changes with javascript credit https://stackoverflow.com/a/52809105/5765524
// history.pushState = ( f => function pushState(){
// var ret = f.apply(this, arguments);
// window.dispatchEvent(new Event('pushstate'));
// window.dispatchEvent(new Event('locationchange'));
// return ret;
// })(history.pushState);
// history.replaceState = ( f => function replaceState(){
// var ret = f.apply(this, arguments);
// window.dispatchEvent(new Event('replacestate'));
// window.dispatchEvent(new Event('locationchange'));
// return ret;
// })(history.replaceState);
// window.addEventListener('popstate',()=>{
// window.dispatchEvent(new Event('locationchange'))
// });
// window.addEventListener('locationchange', function(){
// console.log('location changed!');
// })
let previousUrl = ''
const observer = new MutationObserver(function(mutations) {
if (location.href !== previousUrl) {
previousUrl = location.href
console.log(`Navigated to ${location.href}`)
}
})
const config = { subtree: true, childList: true }
observer.observe(document, config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment