Deep Tech Fusion

tech related weblog

Ajax Cache-Control

A ”’Web cache”’ sits between one or more Web servers (also known as origin servers) and a client or many clients, and watches requests come by, saving copies of the responses — like HTML pages, images and files (collectively known as representations) — for itself.
Then, if there is another request for the same URL, it can use the response that it has, instead of asking the origin server for it again.

When developing with Ajax-based application we need to disable Cache.

This can be done by various different way….

Some of them are discussed here…..

Without Using Headers

* Ajax – Cache Busting with Javascript — [fool the browser]

Browsers are notorious at hanging on tenaciously to cached pages. Even when you use all manner of fancy headers like ””Pragma: no-cache” or “Cache-Control: must-revalidate”” you’ll often find that you receive a cached page rather than a ‘live’ one. Here’s something else you can try:

To the URL which requests the page, ”’add a random (and meaningless) variable and value pair”’. The value should be different each time, and although it will have no effect on the called page, it helps to fool the browser into thinking it has to return a page from a URL it’s never visited before, rather than realising it has it in cache.
A good way to do this in Javascript is to add a random number:
var myDate = new Date();
var myTime = myDate.getTime();
var url=’http://localhost/call_to_ajax.php?rand=’+myTime;‘GET’, url);

Now, each time we call the page whose address is stored in url, we append a meaningless variable rand with a random value,

eg: http://localhost/call_to_ajax.php?rand=321;

* Http: A Caching XmlHttpRequest Wrapper

A wrapper for XmlHttpRequest that supports forced caching on FireFox and forced non-caching on IE.
XmlHttpRequest is a class available in client-side JavaScript for making new HTTP requests from within a page. It is implemented as a built-in object in Safari and FireFox, and as an ActiveX object on Internet Explorer.
More about wrapper class is at’

Download this wrapper from

Using Headers

* PHP – Files

we need to add the following line into the files which will call by open method of object createRequestObject class.

e.g.‘GET’, ‘call_to_ajax.php’);
so we need to add following line into the ”’call_to_ajax.php”’ file.

header(‘Expires: Fri, 09 Jan 1981 05:00:00 GMT’);
header(‘Cache-Control: no-store, no-cache, must-revalidate’);
header(‘Cache-Control: post-check=0, pre-check=0’, FALSE);
header(‘Pragma: no-cache’);

* .htaccess – Files

First enables expires and headers module of apache [httpd.conf]
LoadModule expires_module modules/
LoadModule headers_module modules/

Then add the following lines into .htaccess file
### activate mod_expires
ExpiresActive On
### Expire everything else 1 day before
ExpiresDefault “modification M-3600”
### Apply a Cache-Control header to call_to_ajax.php

Header append Cache-Control “public, must-revalidate”

* PERL – Files

Add the following lines of call_to_ajax.cgi or file
print “Content-type: text/html\n”;
print “Expires: Thu, 29 Oct 1998 17:04:19 GMT\n”;
print “\n”;

References and Further Information
Caching Tutorial
Brian D. Davison’s Web Caching and Content Delivery Resources
Hypertext Transfer Protocol — HTTP/1.1

December 5, 2006 Posted by | AJAX, APACHE, JAVASCRIPT, PERL, PHP | 1 Comment