简体   繁体   中英

Does hashchange event fire when hash is removed?

I used both jquery hashchange and window.addEventListener("hashchange") but the event not firing when hash part is removed by code. I'm using mithril.js to change hash:

m.route('')

window.addEventListener("hashchange", function(evt) {
    console.log("hash change");
    var path = location.hash.substring(1);
    console.log(path);
    //do other things
}, false);

Does the event fire when hash part of the url removed? If not what is the workaround to handle this case?

It doesn't fire if history.pushState() is used to remove the hash.

According to mithril.js docs :

In addition, note that most m.route modes rely on history.pushState in order to allow moving from one page to another without a browser refresh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM