Preventing Content Cache
Content from JavaScript imported from an external file and
content in an iframe are cached.
When the JavaScript or iframe content must change with each
page reload, the cache must somehow be bypassed or prevented
in the first place.
Every browser, it seems, has it's own caching procedures.
What might work for one, might not work for another. If
possible, test caching solutions with each computer
operating system and browser brand as is reasonable to
assume will be used to load your content.
JavaScript caching can be bypassed by using a different
URL with each page reload. Append "?" and a never-repeated
sequence of characters to the URL.
Every time the page loads, the browser sees a different URL
to the external JavaScript file. And that bypasses the cache
assigned to the previous URL.
Here is one way to do that. An example.
Let's assume your current JavaScript is:
We'll append a "?" character and a 13-digit number to the
URL. The number is how many milliseconds have elapsed since
midnight, 01 January 1970, GMT. (JavaScript automatically
keeps track the milliseconds.) This ensures no duplicate
sequence of characters.
As you can see, the unique URL is created, then JavaScript
document.write() is used to write the JavaScript to retrieve
the content from the external file.
Pages containing PHP code and pages with SSI code generally
do not cache. A solution for iframe content caching is to
rename the file with the iframe content to something with a
.php or .shtml extension.
Yet, sometimes it is not feasible to create a new URL with
every page load or to change the web page file name.
I've had success with keeping imported JavaScript from
caching using these three lines, in this order, in the
web page HEAD area:
The same meta tags can be used to prevent caching of web
pages being displayed in an iframe.
If the web page in the iframe is generated with a Perl CGI
script, the cache control directives can be sent to the
browser in the header lines. This is not a reference to the
web page HEAD area, but to the header lines sent to the
browser before the actual web page source code is sent.
Browsers that don't comply with the meta tag caching
directives might do so when the directives are sent to
it in this way.
Try something like this in your CGI script:
That last line is for testing. You would remove the line
when you are ready to go live and, instead, use your own
page printing code.
The time function in the above example will print a 10-digit
number. Within Perl, that number is incremented every
second.
When you test this in an iframe, the number changes every
time the page is reloaded, provided at least one second
elapses between reloads. If the number does not change, your
browser does not comply with the cache control directives.
The only certain way to update content, that I am aware of
to date, is to bypass any previous cache by loading the
content with a URL never before used in the browser. If that
is not feasible, the other techniques described will do a
good job with most browsers.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2007 Bontrager Connection, LLC Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.