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 likehistory.pushState()
except thatreplaceState()
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
Have a Div Move on Scroll Up and Down
Merge Array of Objects by Same Key-Value
Reload Table After Deleting an Item from It With React
How to Show an Alert After Reloading the Page in JavaScript
Executing Nodejs Script File in PHP Using Exec()
One Time Page Refresh After First Page Load
Using JavaScript Node.Js How to Parallel Process for Loops
Replace Multiple Strings With Multiple Other Strings
Discord Bot Not Getting All Users
Ng2-Smart-Table Bind 'Add New' Button Event to an External Button
How to Mock the JavaScript Window Object Using Jest
Uploading Multiple Files Using Formdata()
How to Split Date and Time from the Datetime Variable in JavaScript
Angular: How to Get Default @Input Value
Nodejs MySQL Get Correct Timestamp Format
React Enable Button After All Form Fields Are Not Empty
Uncaught Typeerror: (Intermediate Value)(...) Is Not a Function