Function or API request that tells you when a user has gone to a new link in chrome

Basically title. Is there such a thing that will tell you when you go to a new link in chrome? I was thinking of just doing (window.location.hostname), but there's no way to check if they've actually gone to a new link without having it on an infinite while loop.

Posted Apr 24, 2022, 12:33 AM UTC

11
grantrules•

If you're talking about just clicking any old link, then you would need to use a web extension as the JS running on the page would not be running anymore as you've navigated to a different page. There's the unload event, but it's unreliable: https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event

If you're talking about a SPA that is using the history API, then check out the popstate event: https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event

1