In Javascript, how do I “clear” back (history -1)?

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

The problem is that when the user clicks on it, it will return to the page that redirects.

Is it possible to “undo” the history of the previous page? So, when the user clicks back, instead he returns two pages?

+48
javascript html css
Jan 23 '12 at 10:02
source share
3 answers

Instead of using window.location = url; to redirect

to try:

 window.location.replace(url); 

after using replace (), the current page will not be saved in the history session, that is, the user will not be able to use the "Back" button to navigate to it.

+85
Jan 23 '12 at 10:09
source share

You can use location.replace to replace the current location record (redirect page) with a new one (target). This requires that you redirect via JavaScript, not with meta tags or 302. For example:

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

Live example | Live example source

+8
Jan 23 2018-12-12T00:
source share

For anyone coming to this page and looking for an AngularJS way to do this (and not javascript), use the replace () method of the $ location service):

Use $location.url('/newpath'); or $location.path('/newpath'); as usual for redirection in angular. And then just add $location.replace(); right after him. Or you can link the commands as follows:

 $location.url('/newpath').replace(); 
+4
Apr 01 '16 at 4:50
source share



All Articles