简体   繁体   English

删除哈希后,hashchange事件会触发吗?

[英]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. 我同时使用了jquery hashchange和window.addEventListener(“ hashchange”),但是当哈希部分被代码删除时,该事件不会触发。 I'm using mithril.js to change hash: 我正在使用mithril.js更改哈希值:

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. 如果使用history.pushState()删除哈希,则不会触发。

According to mithril.js docs : 根据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. 另外,请注意,大多数m.route模式都依赖于history.pushState ,以允许在不刷新浏览器的情况下从一页移动到另一页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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