Make Redirect to New Url But Remove Previous from History

Make redirect to new url but remove previous from history

If you want no remnants of the page in the browser history at all, then you cannot send the browser there because it will keep track of all visited pages in the history so it can properly show which links have been visited. As best as I know, there is no way to stop the browser from keeping track of the visited links. If you do a 302 location redirect from the server, both original and redirected page will be in the browser history. The original page will not be in the back-button list, but will be in the history.

If the part of the URL that you are trying to keep less visible is query parameters to the URL, then you can use a form post instead of regular page load because the browser will not store form parameters in the visible part of the history.

Otherwise, you may need to use your URL with an Ajax call (since that will not be stored in the history) and then use client-side window.location.replace(...) to go to the final URL, the idea being that the URL you don't want to share publicly will have never been an URL that the browser page went to, only a URL that was used for an Ajax call.

In Javascript, how do I "clear" the back (history -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.

Redirect with JavaScript but without affecting the history

Use window.location.replace(url) when you don't want to affect history (like a server redirect).

Use window.location = url when you want to affect history.

Remove current page from history browser without using location.replace

All what you want, History has:

  var stateObj = { foo: "bar" };
history.replaceState(stateObj, "page 2", "bar.html");

how to change url without changing browser history

You're looking for replaceState(), it replaces the current position in the history instead of pushing a new one, like pushState() does

history.replaceState({}, 'Title', link.href);

from MDN

history.replaceState() operates exactly like history.pushState()
except that replaceState() modifies the current history entry instead
of creating a new one.

replaceState() is particularly useful when you want to update the
state object or URL of the current history entry in response to some
user action.

Remember, some functions are not available on older browsers. But there is a library
that could help you out.



Related Topics



Leave a reply



Submit