I am doing a simple project, let us take high secure website. I have 5 different JSP pages. If I started from first JSP page, it is redirecting to second JSP page and so on. In the meanwhile, it should not store those pages in my browser history. How to clear those browsing history using JavaScript?
-
16not possible... – HIRA THAKUR Nov 18 '13 at 09:47
-
2`window.location.replace()` ??? – A. Wolff Nov 18 '13 at 09:50
-
1@Jai, see my update. It did not really answer his question, unless he asked the wrong question. – Patrick Kostjens Nov 18 '13 at 09:52
-
1You question is quite confusing. You cannot clear cache, however, you can just add one of your site pages in browser history using location.replace() – A. Wolff Nov 18 '13 at 09:55
-
Is 'disabling back button' what OP requested? most answers proposed seem to focus on this - I'm not so sure it's congruent with 'clearing browsing history' per title of OP's Q... i.e. if I redirect webpage and select options, privacy/security, clear browsing history- will all items already be cleared? if you press alt+h, will this window show 0 records? hmmmm... – JB-007 Oct 24 '21 at 15:39
7 Answers
Can you try using document.location.replace()
it is used to clear the last entry in the history and replace it with the address of a new url. replace()
removes the URL of the current document from the document history, meaning that it is not possible to use the "back" button to navigate back to the original document.
<script type="text/javascript">
function Navigate(){
window.location.replace('your link');
return false;
}
</script>
HTML:
<button onclick="Navigate()">Replace document</button>

- 22,583
- 7
- 50
- 59
-
3
-
-
@Qantas94Heavy ya both work but: http://stackoverflow.com/questions/7857878/window-location-vs-document-location – A. Wolff Nov 18 '13 at 09:57
-
-
idea is instead clicking link, you can call a javascript function. In that function you can replace the url to be navigate. i.e document.location.replace('url here'); it clear the current history and navigate. – Krish R Nov 18 '13 at 10:23
-
It is like anchor tag..I need those history should not be visible in the browser history – Dinesh Kumar Nov 18 '13 at 10:31
-
"The replace() method replaces the current document with a new one. ...replace() removes the URL of the current document from the document history, meaning that it is not possible to use the "back" button to navigate back to the original document." That is it is used to navigate to a new document! – Paul Zahra Jun 16 '16 at 12:45
-
"meaning that it is not possible to use the "back" button to navigate back to the original document" I guess it should be noted that you can still use the `Back` button (and `Forward` button) to navigate to other pages in the history, before and after "the original document" (if there are any). You just can't navigate to "the original document" page, because you replaced it. – Kevin Fegan Nov 19 '18 at 16:23
-
@Qantas94Heavy - That question has (did have) good answers for this, but it was deemed a duplicate and its answers were merged with another question, here: [What's the difference between window.location and document.location in JavaScript?](https://stackoverflow.com/q/2430936/606539). An archive of the original (perhaps edited) question you linked to (with its answers) can be found here: [window.location vs. document.location (Archive.org)](http://web.archive.org/web/20130802055634/https://stackoverflow.com/questions/7857878/window-location-vs-document-location). – Kevin Fegan Nov 19 '18 at 16:58
-
but does not stop the user holding down the back button and selecting page from earlier in the session – Snapey Jan 26 '20 at 11:18
As MDN Window.history() describes :
For top-level pages you can see the list of pages in the session history, accessible via the History object, in the browser's dropdowns next to the back and forward buttons.
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.
So there is no Javascript method to clear the session history, instead, if you want to block navigating back to a certain page, you can use the location.replace() method, and pass the page link as parameter, which will not push the page to the browser's session history list. For example, there are three pages:
a.html:
<!doctype html>
<html>
<head>
<title>a.html page</title>
<meta charset="utf-8">
</head>
<body>
<p>This is <code style="color:red">a.html</code> page ! Go to <a href="b.html">b.html</a> page !</p>
</body>
</html>
b.html:
<!doctype html>
<html>
<head>
<title>b.html page</title>
<meta charset="utf-8">
</head>
<body>
<p>This is <code style="color:red">b.html</code> page ! Go to <a id="jumper" href="c.html">c.html</a> page !</p>
<script type="text/javascript">
var jumper = document.getElementById("jumper");
jumper.onclick = function(event) {
var e = event || window.event ;
if(e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = true ;
}
location.replace(this.href);
jumper = null;
}
</script>
</body>
c.html:
<!doctype html>
<html>
<head>
<title>c.html page</title>
<meta charset="utf-8">
</head>
<body>
<p>This is <code style="color:red">c.html</code> page</p>
</body>
</html>
With href link, we can navigate from a.html to b.html to c.html. In b.html, we use the location.replace(c.html)
method to navigate from b.html to c.html. Finally, we go to c.html*, and if we click the back button in the browser, we will jump to **a.html.
So this is it! Hope it helps.
It's not possible to clear user history without plugins. And also it's not an issue at developer's perspective, it's the burden of the user to clear his history.
For information refer to How to clear browsers (IE, Firefox, Opera, Chrome) history using JavaScript or Java except from browser itself?
-
9Is that ever a useful answer? 1) Security is always the developer's concern especially in the example given. 2) It's roughly possible to solve the problem one piece at a time as outlined in other answers here. 3) a dev may not be able to block a maliciously logged in sniffer, but they should do their best to clean up for a user who has logged out or moved on and forgotten to log out. – ebyrob Dec 22 '16 at 16:34
-
It's possible to clear the browsing history using Chrome's [history API](https://developer.chrome.com/docs/extensions/reference/history/), but this can only be used in browser extensions. – Anderson Green Aug 07 '21 at 17:13
No,that would be a security issue.
However, it's possible to clear the history in JavaScript within a Google chrome extension. chrome.history.deleteAll().
Use
window.location.replace('pageName.html');
similar behavior as an HTTP redirect
Read How to redirect to another webpage in JavaScript/jQuery?

- 1
- 1

- 58,085
- 24
- 103
- 107
-
1
-
http://go4answers.webhost4life.com/Example/clear-browser-history-thru-javascript-158595.aspx – Dinesh Kumar Nov 18 '13 at 09:50
-
3*not* clearing the history is a security issue. Clearing the history is, generally speaking, a minor inconvenience. (somewhere between the blink tag and a popup window in disruption factor) – ebyrob Dec 22 '16 at 16:23
to disable back function of the back button:
window.addEventListener('popstate', function (event) {
history.pushState(null, document.title, location.href);
});

- 4,100
- 37
- 31
-
Working fine for me, but not on chrome for some reasons Thank you – khaled Dehia Jun 24 '20 at 01:17
Ok. This is an ancient history, but may be my solution could be useful for you or another developers. If I don't want an user press back key in a page (lets say page B called from an page A) and go back to last page (page A), I do next steps:
First, on page A, instead call next page using window.location.href
or window.location.replace
, I make a call using two commands: window.open
and window.close
example on page A:
<a href="#"
onclick="window.open('B.htm','B','height=768,width=1024,top=0,left=0,menubar=0,
toolbar=0,location=0,directories=0,scrollbars=1,status=0');
window.open('','_parent','');
window.close();">
Page B</a>;
All modifiers on window open are just to make up the resulting page. This will open a new window (popWindow) without posibilities of use the back key, and will close the caller page (Page A)
Second: On page B you can use the same proccess if you want this page do the same thing.
Well. This needs the user accept you can open popup windows, but in a controlled system, as if you are programming pages for your work or client, this is easily recommended for the users. Just accept the site as trusted.
You cannot clear the browser history. It belongs to the user, not the developer. Also have a look at the MDN documentation.
Update: The link you were posting all over does not actually clear your browser history. It just prevents using the back button.

- 5,065
- 6
- 29
- 46
-
-
@JackMiller "You" being the developer of a website/web application. – Patrick Kostjens Jan 26 '21 at 19:51