How to “browser back” when using pushState?

前端 未结 2 1218
终归单人心
终归单人心 2021-01-16 06:06

I have this code:

window.history.pushState(newUrl, \"\", newUrl);

My question is, how to make sure that when doing pushState the browser ba

2条回答
  •  日久生厌
    2021-01-16 06:34

    The normal behaviour for the back button is for the browser to go back to the previous document, but when you use pushState, there isn't a previous document.

    The point of pushState is to keep the browser on the same document while updating the URL. This is accompanied by DOM changes applied with JavaScript.

    It is a simulation of going to a new page.

    To make the back button appear to work, you need to write a matching simulation of going to the previous page.

    You can do this by listening for a popstate event.

    Page 1
    
    
    
    
    

提交回复
热议问题