Change hash without triggering a hashchange event

后端 未结 2 454
借酒劲吻你
借酒劲吻你 2021-02-04 23:30

I\'m using the hash to load content dynamically. To make the back button work I am capturing hash changes. However sometimes I need to change the hash without triggering the has

2条回答
  •  青春惊慌失措
    2021-02-05 00:28

    You could use history.replaceState and append the hash, to replace the current URI without triggering the hashchange event:

    var newHash = 'test';
    
    history.replaceState(null, null, document.location.pathname + '#' + newHash);
    

    JSFiddle example

提交回复
热议问题