在Javascript中,如何“清除”后面(历史记录-1)?

时间:2022-06-21 07:15:02

When the user loads the page, I immediately do a window redirect to another location.

当用户加载页面时,我立即将窗口重定向到另一个位置。

The problem is, when the user clicks back, it'll go back to the page which does the redirect.

问题是,当用户点击回来时,它将返回执行重定向的页面。

Can I "cancel" the history of the previous page? So that when the user clicks back, it goes back TWO pages instead?

我可以“取消”上一页的历史记录吗?那么当用户点击回来时,它会返回两页而不是?

3 个解决方案

#1


82  

Instead of using window.location = url; to redirect,

而不是使用window.location = url;重定向,

try:

尝试:

window.location.replace(url);

after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

使用replace()后,当前页面将不会保存在会话历史记录中,这意味着用户将无法使用“后退”按钮导航到该页面。

#2


8  

You can use location.replace to replace the current location entry (the redirect page) with the new one (the target). That requires that you do the redirection via JavaScript rather than with meta tags or a 302. E.g.:

您可以使用location.replace将当前位置条目(重定向页面)替换为新目标条目(目标)。这要求您通过JavaScript而不是使用元标记或302进行重定向。例如:

// In the redirecting page
location.replace("path/to/target/page");

Live example | Live example source

实例|实例示例源

#3


4  

For anyone coming across this page and looking for an AngularJS way to accomplish this (rather than javascript), use the $location service's replace() method (documentation) :

对于访问此页面并寻找AngularJS方法来实现此目的(而不是javascript)的任何人,请使用$ location服务的replace()方法(文档):

Use $location.url('/newpath'); or $location.path('/newpath'); as you normally would to do the redirection in angular. And then just add $location.replace(); right after it. Or you can chain the commands like this:

使用$ location.url('/ newpath');或$ location.path('/ newpath');正如你通常那样在角度方面进行重定向。然后只需添加$ location.replace();就在它之后。或者你可以像这样链接命令:

$location.url('/newpath').replace();

#1


82  

Instead of using window.location = url; to redirect,

而不是使用window.location = url;重定向,

try:

尝试:

window.location.replace(url);

after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

使用replace()后,当前页面将不会保存在会话历史记录中,这意味着用户将无法使用“后退”按钮导航到该页面。

#2


8  

You can use location.replace to replace the current location entry (the redirect page) with the new one (the target). That requires that you do the redirection via JavaScript rather than with meta tags or a 302. E.g.:

您可以使用location.replace将当前位置条目(重定向页面)替换为新目标条目(目标)。这要求您通过JavaScript而不是使用元标记或302进行重定向。例如:

// In the redirecting page
location.replace("path/to/target/page");

Live example | Live example source

实例|实例示例源

#3


4  

For anyone coming across this page and looking for an AngularJS way to accomplish this (rather than javascript), use the $location service's replace() method (documentation) :

对于访问此页面并寻找AngularJS方法来实现此目的(而不是javascript)的任何人,请使用$ location服务的replace()方法(文档):

Use $location.url('/newpath'); or $location.path('/newpath'); as you normally would to do the redirection in angular. And then just add $location.replace(); right after it. Or you can chain the commands like this:

使用$ location.url('/ newpath');或$ location.path('/ newpath');正如你通常那样在角度方面进行重定向。然后只需添加$ location.replace();就在它之后。或者你可以像这样链接命令:

$location.url('/newpath').replace();