How to Clear browser history using javascript?

How to clear browser history .

Clear Browser History using Javascript

If you need to clear the Browser History, after arriving on a particular page (e.g. arriving to Login page after Logout), below is the wicked way. This script needs to put in only on one page, from where you do not want to move back.

<script type="text/javascript">
window.onload = function () { Clear(); }
function Clear() {
var Backlen=history.length;
if (Backlen > 0) history.go(-Backlen);
}
</script>

Second, if you do not want the user to move to the back page, you can do as below. This script needs to be repeated at every page.

<script type="text/javascript">
if(window.history.forward(1) != null)
window.history.forward(1);
</script>

<script type = "text/javascript" >
function disableBackButton()
{
window.history.forward();
}
setTimeout("disableBackButton()",0);
</script>

<body onunload="disableBackButton()" onload="disableBackButton()">
</body>

Clearing browser history from javascript after logout

Clearing browser history was eating my head. After the user clicks the logout from the portal and clicks on the back button from the browser, it get back the previous page. Finally I did it by the below steps, which I found to be interesting.

Step 1. Add the below javascript code on the page load
<script>
function callme()
{
history.go(1);
}
</script>
Step 2. Dont allow the page to be saved in cache. Add the below lines between <head>

<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
Step 3. On click of Logout. Clear the history from the browser and redirects to Login page from server side.

string strScript = "<script>var Backlen=history.length;history.go(- Backlen);window.location.replace('Login.aspx');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "ClearHistory", strScript);

<script type="text/javascript">
function noBack()
{
window.history.forward()
}
noBack();
window.onload = noBack;
window.onpageshow = function(evt) { if (evt.persisted) noBack() }
window.onunload = function() { void (0) }
</script>




Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here