location.hash and back history

前端 未结 2 549
臣服心动
臣服心动 2020-12-15 07:42

Is there a function that can be called to prevent the browser from recording a back history entry when changing the hash value?

I am writing a simpl

相关标签:
2条回答
  • 2020-12-15 08:13

    You can use replaceState().

    Before you change the hash you save the history, then you change your hash, finally you replace the history with the one you saved.

    Alternatively you can use popState Event.

    0 讨论(0)
  • 2020-12-15 08:19

    window.location.replace will let you set the url without adding it to the browser history.

    var baseUrl = window.location.href.split('#')[0];
    window.location.replace( baseUrl + '#' + imageID );
    

    documentation

    0 讨论(0)
提交回复
热议问题