1

I am working on one project which has lot pf pages..

My pages structure is as follows: login >> dashboard >> add >> view >> edit and so on... Problem I am facing is : If I clicked back button and I am on login page then again If I have clicked back button it is redirecting to dashboard page again.

What I want is: I want to clear browsers history or it should not go to dashboard page from login page if I pressed back button from login page.

I tried lot of code snippets,all code I have added in login page: Some of them are below:

Javascript code: 1)

var Backlen=history.length;   
history.go(-Backlen);   
window.location.href=contextPath + "login.jsp";

2)

function preventBack(){window.history.forward();}
setTimeout(preventBack(), 0);
window.onunload=function(){null};

3)

function disableBack() { window.history.forward() }
window.onload = disableBack();
window.onpageshow = function(evt) { if (evt.persisted) disableBack() }

Also tried Java code as well- cleared sessions using below code:

<%  
String forwardUrl = "";
if(sessionTimeout!=null)
{
request.setAttribute("timeoutMessage", sessionTimeout);
forwardUrl="login.jsp?locale="+locale+"&timeoutMessage="+sessionTimeout;
}else
{
forwardUrl="login.jsp?locale="+locale;  
request.getSession().removeAttribute(sessionTimeout); 
}
%> 
Monica
  • 65
  • 1
  • 8
  • 1
    Sorry but that's not possible. See here: http://stackoverflow.com/questions/20044554/how-to-clear-browsing-history-using-javascript – Paul Nov 07 '14 at 11:26
  • Get it straight: the browser is the property of the user, it is not yours to manipulate and mangle. That history is there, it is going to stay there and there is nothing you can do about it other than build your web application properly such that it doesn't matter. – Gimby Nov 07 '14 at 11:36
  • atleast I can clear session value when I am redirected to login page? – Monica Nov 07 '14 at 11:42
  • Using Ajax allows you to update your page without going forward. That might be a way to go. – Jasper de Vries Nov 07 '14 at 13:08
  • @JasperdeVries ok I will check that..thanks – Monica Nov 10 '14 at 07:47

1 Answers1

0

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.

Paul
  • 175
  • 1
  • 13