how to force no-cache in scripts
I've got a little question here. Is there any chance of forcing a browser not to cache a js script? My page is in HTML and I can clear the cache on page load every time, but some of the sections would stop working correctly. The script is below. It's a simple cash converter and current exchange rates I've found, but it's not updating unless I will hot Ctrl+F5.
Since the script you are downloading is generated by a PHP program you might try having the PHP script to send an "expires" header with a date set to sometime in the past.
I don't know if that will work or not. A "no-cache" header is ignored by certain browsers so you can't depend on that.
You could append a random number/string to the end of the script URL:
This won't stop the caching but it should load the script every time (because the URL is always a new one).
alll browsers WILL not cache a url if ALL of the following are true:
- the response has an Expires header in the past, present, or very-near future
- the response has a Date header
- the response status code is 200
- certain proxies want Cache-Control: Private to avoid local caching.
if a 304 slips out, the browser gets paranoid and usually pings the script url going forward.
at that point, if your Date and Expires are different, or the content-length has changed, and yet expires is in the past, a new copy should be shipped.
you can bank on that algo by avoiding 304s altogether.
other things like etags, pragma, can help good caching, but don't really work to prevent caching.
the url creeping of using a random() or timestamp in the url pollutes the cache and purges useful content, don't do it.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)