Forum Moderators: open

Message Too Old, No Replies

Stop external JS from caching

Help!

         

Gullanian

12:44 am on May 23, 2005 (gmt 0)

10+ Year Member



Can anyone help with my predicament?

I'm writting a FireFox extension. In my RDF file I have the line:

<script type="application/x-javascript" src="http://www.c4sms.com/fireFox/getaddresses.asp?cachecontrol=" />

This script must NOT be cached, and loads dynamic content from a database, hence it must reside on the server.

The only way I know of to stop a external script from caching is to pass a parameter through to it, for example the date and time.

How do I pass this variable in with that line above? I want the cachecontrol paramter to print out a variable.

Putting a <Script> in a <script> to document.write the variable isn't allowed, I had no luck with document.addElement either!

In the ASP file I've even added:

Response.Expires = 60
Response.Expiresabsolute = Now() - 1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"

But this isn't working either. It's still caching, and the feature I am building is useless if it's calling a cached version.

Is there some easy way I am missing to stop this from caching?

Thanks for any help!

Tom

CgiBin

4:21 pm on May 23, 2005 (gmt 0)

10+ Year Member



If the page generating the HTML that calls the .js is also rendered by asp or php you can add a dummy variable in the request string. I recommend using a combination of pid and time since in theory several requests could come in at the same time.

in php its:
$foo = time() . getmypid();

Then your js call would be:
<script type="application/x-javascript" src="http://www.c4sms.com/fireFox/getaddresses.asp?cachecontrol=<?=$foo?>" />

However the problem snowballs because the user probably will end up caching the source HTML, so you end up having to call it with some sort of cachecontrol... etc etc etc.

The only sure way to avoid all that is to send additional headers via the web server that instructs the users' browser to not cache, and you also have to worry about all the "web-accelerator" ISPs too since all that is is a cache too!

If using apache put the following in your httpd.conf:

Header set Cache-control "private, no-cache, no-store, must-revalidate, post-check=0, pre-check=0"
Header set Expires "Sat, 1 Jan 2000 05:00:00 GMT"
Header set Pragma "no-cache"

I'm not too familiar with asp, but you might be able to have just that page send extra headers as well. You'd want to send the same 3 headers above BEFORE you send the content/type and any output.