Search code examples
javascripturlhashchange

How to know when a url hash has been manuallly (writing) changed?


is there a way, or a tool (js plugin or other) that let me know when I manually change the hash part of the URL? Note: with 'manual' i mean, I'm here:

http://www.[mydomain].com/#hash1

and then I cancel and rewrite the hash with mouse/keaboard, OR, clicking back/next browser buttons.

http://www.[mydomain].com/#hash2

Solution

  • window object has a hashchange event that is fired when url hash changes

    window.addEventListener("hashchange", function(event){
     console.log('hash change event =>',event);
    }, false);
    

    Read more from mdn documentation - https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onhashchange