In Javascript, how do I "clear" the back (history -1)?
Solution 1:
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.
Solution 2:
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
Solution 3:
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();