Nir*_*Nir 2 javascript redirect
我有一个嵌入在随机站点中的小部件。当用户点击时,我调用服务器更新发生的点击,服务器返回重定向到正确的页面。问题是浏览器的后退按钮不再起作用。
我使用 location.href 进行重定向。
我尝试了 location.replace -它保留了后退按钮,但它会转到单击小部件的页面之前的页面(它用新页面替换它)。
如何在向服务器报告后进行页面切换并仍然允许后退按钮工作?
您无法返回自动重定向的页面。这只是一个坏掉的后退按钮和一个沮丧的用户。
User goes to page A.
User clicks on link to page B.
Page B automatically redirects to page C with window.location.
User hits back button and momentarily goes back to page B.
Page B automatically redirects to page C with window.location.
User hits back button and momentarily goes back to page B.
Page B automatically redirects to page C with window.location.
User is sad.
Run Code Online (Sandbox Code Playgroud)
显然这行不通。它必须像这样工作:
User goes to page A.
User clicks on link to page B.
Page B automatically redirects to page C with window.location.replace().
User hits back button and goes back to page A.
User is happy.
Run Code Online (Sandbox Code Playgroud)
如果您希望后退按钮起作用,您必须使用 window.location.replace() 自动重定向。