JS - window.history - Delete a state

后端 未结 2 597
面向向阳花
面向向阳花 2020-11-30 23:35

Using the html5 window.history API, I can control the navigation pretty well on my web app.

The app currently has two states: selectDate(1)

2条回答
  •  心在旅途
    2020-12-01 00:22

    There is no way to delete or read the past history.

    You could try going around it by emulating history in your own memory and calling history.pushState everytime window popstate event is emitted (which is proposed by the currently accepted Mike's answer), but it has a lot of disadvantages that will result in even worse UX than not supporting the browser history at all in your dynamic web app, because:

    • popstate event can happen when user goes back ~2-3 states to the past
    • popstate event can happen when user goes forward

    So even if you try going around it by building virtual history, it's very likely that it can also lead into a situation where you have blank history states (to which going back/forward does nothing), or where that going back/forward skips some of your history states totally.

提交回复
热议问题