html - In Javascript, how do I "clear" the back (history -1)? - Stack Overflow

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

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?

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?

Share Improve this question asked Jan 23, 2012 at 10:02 TIMEXTIMEX 271k366 gold badges799 silver badges1.1k bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 113

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

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.

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.:

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

Live example | Live example source

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) :

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').replace();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1737055656a3920081.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信