How to get the previous URL in JavaScript?

后端 未结 7 796
野趣味
野趣味 2020-11-22 06:05

Is there any way to get the previous URL in JavaScript? Something like this:

alert(\"previous url is: \" + window.history.previous.href);

7条回答
  •  再見小時候
    2020-11-22 06:31

    If you are writing a web app or single page application (SPA) where routing takes place in the app/browser rather than a round-trip to the server, you can do the following:

    window.history.pushState({ prevUrl: window.location.href }, null, "/new/path/in/your/app")
    

    Then, in your new route, you can do the following to retrieve the previous URL:

    window.history.state.prevUrl // your previous url
    

提交回复
热议问题