Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 802
TIMEX Avatar asked Jan 23 '12 10:01

TIMEX


People also ask

Can JavaScript clear browser history?

You cannot clear the browser history. It belongs to the user, not the developer.

What is history back in JavaScript?

The history. back() method loads the previous URL (page) in the history list. The history.


2 Answers

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.

like image 147
xdazz Avatar answered Sep 22 '22 11:09

xdazz


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

like image 45
T.J. Crowder Avatar answered Sep 22 '22 11:09

T.J. Crowder