How to expire page on back button click?

19,875

Solution 1

You cannot expire a page on back button of browser as long as sessions aren't involved.Its there for back browsing of pages which many users do, so it depends on how you want user to browse your site. You actually need to handle the back button of browser. There are few ways to do it. Prominent one is using this code in javascript:`

window.history.forward();

` but it will forward your back request to current page in history The one that worked for me is:

<script type="text/javascript">
function noBack(){
            location.replace(logouturl);
            window.location="logouturl";
}

HTML

<input type="button" value="SignOut" onclick="noBack()" align="middle">

Solution 2

Try this

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml" >
  <title>Untitled Page</title>
  <script type = "text/javascript" >
  function changeHashOnLoad() {
 window.location.href += "#";
 setTimeout("changeHashAgain()", "50"); 
 }

function changeHashAgain() {
window.location.href += "1";
}

var storedHash = window.location.hash;
 window.setInterval(function () {
 if (window.location.hash != storedHash) {
     window.location.hash = storedHash;
}
}, 50);


</script>
 </head>
 <body onload="changeHashOnLoad(); ">
  Try to hit back!
 </body>
 </html>`
Share:
19,875
Rajesh
Author by

Rajesh

I am an enthusiastic developer who believe in learning each day as soul way to reach prosperity.

Updated on June 04, 2022

Comments

  • Rajesh
    Rajesh almost 2 years

    I have added following meta tag in jsp page

    <html>
    <head>
    <title>xyz</title>
    <link type='text/css' rel="stylesheet" href="sdsds/sdsd"/>
    <meta content="max-age=0" http-equiv="cache-control">
    <meta content="no-store" http-equiv="cache-control">
    <meta content="-1" http-equiv="expires">
    <meta content="Tue, 01 Jan 1980 1:00:00 GMT" http-equiv="expires">
    <meta content="no-cache" http-equiv="pragma">
    </head>
    <body><h1>jsdsdsds</h1>
    <a href="abc">click me</a>
    </body>
    </html>
    

    After navigating by clicking link "click me" I am opening a new page. When I click back button it is working..I want the page to expire.. Note:In both jsp page I have added same meta tag. I tried adding this

    <%@ page import="java.lang.*" %>
    <%
    // Set to expire far in the past.
    response.setHeader("Expires", "Sat, 6 May 1971 12:00:00 GMT");
    // Set standard HTTP/1.1 no-cache headers.
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    // Set standard HTTP/1.0 no-cache header.
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
    %>
    

    both above HTML and inside Head..still I can see back page., Tested in mozilla firefox 10.0 and IE8 Any suggestion?