undefinedfix
Sign in

In mobile browser, modify location.href There is no historical record as expected

user9532756 edited in Thu, 26 Jan 2023

I want to realize such a requirement: the page address contains the parameter PID. The existence of PID means that after opening the current page, it immediately jumps to an external page corresponding to PID. When the user retreats from the external page, he can return to the current page and display the page content normally.

My implementation is: storing PID with hash. In the page entry JS, if PID is found in the hash, the URL corresponding to the PID is obtained from the background through Ajax, and then it is downloaded from the location.hash Remove the PID from the list, and then modify it location.href Make a jump.

However, the actual measurement found that in the mobile browser, no matter when I jump to the page, when I press back, the tab will directly return to the original state, leaving no history.

The following methods have been tried, but none of them work:

  1. After modifying hash, add setTimeout delay for 2 seconds and then jump
  2. use location.assign
  3. use history.pushState Manually write the current page URL and then jump
  4. stay window.onload Execute judgment and delay jump

But I found a strange phenomenon. Before the delay jump, if you manually click any position on the page, the operation code of the above history can take effect, and you can return to the new page normally. However, the user click operation simulated by dispatchEvent is still not good.

Are there any hidden rules about how to jump and generate historical records?

https://html.spec.whatwg.org/multipage/history.html#location -The description searched by object setter navigator here shows that: 2 and 3 cases are caused by the subsequent action of click event triggered by the user. Location modification can generate history. But 1 (the document corresponding to location is fully loaded) should be OK. If it does not refer to after the onload event, how can it be fully loaded

0 Replies