Ajax with history.pushState and popstate - what do I do when popstate state property is null?

前端 未结 6 931
遥遥无期
遥遥无期 2021-02-07 01:18

I\'m trying out the HTML5 history API with ajax loading of content.

I\'ve got a bunch of test pages connected by relative links. I have this JS, which handles clicks on

6条回答
  •  清酒与你
    2021-02-07 01:48

    This is an older question but there is a much simpler answer using native javascript for this issue.

    For the initial state you should not be using history.pushState but rather history.replaceState.

    All arguments are the same for both methods with the only difference is that pushState creates a NEW history record and thus is the source of your problem. replaceState only replaces the state of that history record and will behave as expected, that is go back to the initial starting page.

提交回复
热议问题