How to clear browsers' cache with javascript?

Dear All,

how to clear browsers’ cache with javascript? … so users will unable to go to previous page after they logged out…

I tried to add <meta http-equiv=“Pragma” content=“no-cache”> on head section of my page1.jsp … but it does not work


<&#37;@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<html>
    <head>
        <meta http-equiv="pragma" content="no-cache" />
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Success</title>
    </head>
    <body>
        <h1>Congratulations!</h1>
        <p>You have successfully logged in.</p>
        <p>Your name is: <bean:write name="LoginForm" property="name" />.</p>
        <p>Your email address is: <bean:write name="LoginForm" property="email" />.</p>
        <p></p>
        <a href="Logout.do">Logout</a>
    </body>
</html>


If you don’t want them to be able to go back to the previous page, you can achieve that not with javascript, but with a server-side redirect that redirects the from the previous page to the new page.

If you replace the anchor with an onclick handler you can use location.replace, which replaces the current page in history with the new page. The following script lets you save or remove the history.

<html>

<head>

<style type=“text/css”>
<!–
body { font-family:arial; font-size:14px; color:#000080; font-weight:bold; }
.curs { cursor: pointer; }
–>
</style>
</head>

<body>

<p class=“curs” onclick=“location.href=‘logout.htm’”>Logout leaving history</p>
<p class=“curs” onclick=“location.replace(‘logout.htm’)”>Logout removing history</p>

</body>

</html>

I wouldn’t use javascript for something like that.

It wouldn’t work for users with javascript turned off for any reason.

I to would do it server side.

btw - if the user’s session is properly terminated when they log out, it shouldn’t matter if they immediately go back to the previous page because they will then get a “Session Expired” (or something similar) error message in their browser by default.

Javascript for it is some of the worst advice I’ve seen in a while… especially since for what you asked it wouldn’t even work.

What you are looking to do is, as Kalon said, best handled server-side, specifically using HTTP response headers.

If you are using PHP, that’s what the ‘header’ function is for.

http://php.net/manual/en/function.header.php

See their second example:


<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

Should work flawless every time. In addition to that if this is a form/submit you don’t want repeating, I would store a semi-random hash in a database md5(random.IP Addy.timestamp) and in a hidden input on the form on the page you don’t want them going back to. When they submit the first time, delete it from the database. If the hash is submitted and isn’t in the database, you’ll know the form was submitted more than once.

Oh, BTW – you have to send headers BEFORE any markup is sent. Just figured I’d point that out.