Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

When I right-click the navigate back arrow in my web browser, I can see a list of previous URLs.

Is there a way to clear that history from code in a web app?

All the best.

What I have tried:

Searched all over the Internet, and except for some JavaScript code that is not recommended, I did not find any possible solution.
Posted

I did a quick Google Search: js clear navigation history - Google Search[^]

Found the following answer: Window: history property - Web APIs | MDN[^]
Quote:
For security reasons the History object doesn't allow the non-privileged code to access the URLs of other pages in the session history, but it does allow it to navigate the session history.

There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL.
 
Share this answer
 
Comments
Perić Željko 30-Mar-24 6:40am    
Solution proposed in the article at second URL is only for Mozilla.
Thanks for effort finding solution.
Perić Željko 30-Mar-24 6:43am    
Related to first URL:
You cannot remove the entire back button history. All you can do is replace the last entry with the next page, using window.location.replace('url');
I have already checked that. but I was interested is there a way to do it not by using JavaScript, but for example some C# code...

All other solutions are almost the same and relates to using JavaScript that is not recommended.
Graeme_Grant 30-Mar-24 6:51am    
Yes, as per the posted quoted text above from the link provided.
To directly answer the question of a "web app", no, you cannot touch the browser, or any objects in it, like navigation history, from your server-side code.

Grant has given you the only options you have from non-privileged code, which does not clear the history the way you want. Doing so would be a security risk, believe it or not.
 
Share this answer
 
Comments
Perić Željko 31-Mar-24 4:47am    
Thanks
If you want to annoy users, come up with a way to prevent them from being able to go back from your site. There is a way that you could, sort of, achieve this which is to open a new browser tab when you navigate into your site. Your users will hate it though.
 
Share this answer
 
Comments
Perić Željko 31-Mar-24 4:47am    
Thanks
Pete O'Hanlon 31-Mar-24 5:03am    
You are most welcome.
Richard Deeming 3-Apr-24 4:54am    
The most common annoying one I see is loading a page which then immediately redirects to the real page. Click the back button, and you're immediately redirected to the same page. You have to use the drop-down menu, which many users don't know about, to actually navigate back to the previous page/site.

It's the sort of behaviour that will result in the site ending up in the "dark patterns" hall of shame.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900