Stop user from viewing the previous page once logged out on pressing browse back button

Yatendra

New Member
Hello Everyone,

I am facing issue in my application’s logout scenario. After the user login into website, he/she uses it(website) and when done, they do a logout, which leads them back to login page. But the problem is now, from this login page, if I click the browser back button then it again takes the user back to the previous visited page as if logged in. How can I stop user from viewing the previous page once logged out?

Thanks in advance.
 

Cecil

19+ years progress programming and still learning.
Once in a while nearly every web application developer comes across this problem and I had exactly the same issue as you. My solution was a mix of javascript with JQuery and it seam unobtrusive, meaning you don't need to make too many changes to your existing html.

So hear goes:
On your "Logout" anchor link (I'm guessing you are using a anchor ) add an id attribute of "LogoutAction" so it should look something like this:

Code:
<a id="LogoutAction" href="`APPURL`/LOGOUT" title="Logout">Logout'</a>

Then add a new block of JavaScript code into your existing HTML (generally in the <head></head> section of the HTML) code:

Code:
<script type="text/javascript">

$(function() {

    $("#LogoutAction").on('click', function(event){

      event.preventDefault();

      var url = $(this).attr('href');

     window.location.replace( url );
    });
});

</script>

Then if you are not already using JQuery (and who isn't these days) also include the following script element into the <head></head> section and that's it.....

Code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

So what actually happens then. JQuery is effectively waiting for a 'click' event of the Logout link. JQuery will extract the 'href' attribute into an JavaScript variable. Then using JavaScript use the 'window.location.replace' method, passing in the url vailable and that resets/clears the browser history. So when the user clicks the back button there are essentially at the first position in the browsers history.

I'm probably not explaining this very well, but it does seam to work. I need to do more testing in other browsers but it does work under Firefox.

Hope this helps. Have fun.

Other references:
http://www.w3schools.com/jsref/met_loc_replace.asp
 
Top