How to prevent browser caching

There are many ways you can prevent browser caching.
One of them is to include following meta tags, place these tags in head section of html document.

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">

For ASP pages use following script at the extreme beginning of the page

<% Response.CacheControl = "no-cache" %>
<% Response.AddHeader "Pragma", "no-cache" %>
<% Response.Expires = -1 %>

In case of JSP pages use following code

<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", -1);
%>

If you are generating your response in java or some other languages then you need to set following parameters in response header.

response.addHeader("cache-control", "no-cache");
response.addHeader("pragma", "no-cache");
response.addHeader("expires", "-1");

All of the above method works on the response of the particular page and tell browser not the cache the response.

There is another way of telling browser to make a fresh request and don’t use the cached content for the page if any. This is done by appending some random characters to requested url. Below is sample code

<script>
var url = "http://techpitcher.com";
url += "?preventCache="+ Math.floor(Math.random()*100000);
</script>

This will create a random url every time and won’t allow browser to use cache content.

Related Post

Restoring Conventional Browser Navigation to AJAX Applications
What is watchdog timer?
Sun turns JavaScript into Java. Google turns Java into JavaScript
jQuery – A lightweight JavaScript Library
Compression using mod_deflate in Apache

Comments

Leave a Reply