How do I insert an entry into browsing history via JavaScript

前端 未结 2 2050
囚心锁ツ
囚心锁ツ 2021-01-14 11:02

How do I insert an entry into browsing history so back button goes to different page 1st click then original page on 2nd click?

So if you need a good explanation of

2条回答
  •  旧时难觅i
    2021-01-14 12:03

    You can't directly manipulate the browsing history.

    Such a feature would be seen as a security hole (and it would be), so I doubt that any browsers would ever implement it.

    You might be able to hack around it however by doing something like this:

    NOTE: This entirely hinges around the assumption that the referrer will get changed by the back button. I don't think this actually happens, so it more than likely won't work, but hey.

    You have two pages, PageA and PageB.

    • The user hits PageA
    • The page (on the client, using javascript) checks the HTTP referrer, and if it is not PageB, then it immediately redirects the user to PageB.
    • Now that you're on PageB, if the user clicks the back button, it will go back to PageA.
    • PageA will check the referrer, it willmay be PageB, so there is no redirect.

提交回复
热议问题