Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: Caching


Message #1 by "Chris Thompson" <mail@t...> on Wed, 19 Dec 2001 10:00:09 -0700
Its very tempting to want to eliminate caching altogether to ensure that the
latest files are always used, but one musn't lose sight of the reason why
caching exists in the first place - for performance.  If you have big js
files containing lots of functions, ideally you want users to use their
cached versions unless the file has actually changed, in which case you want
to force them to download the newer version.

Unfortunately we cannot rely on internet explorer for this.  By default the
caching setting is "automatic" - in my experience this setting NEVER
downloads the latest files.  You can inform your users to change their
settings to download files every time IE is started, but for a start you
can't rely on them to do this, and if they do they will suffer a performance
hit every time they log onto your application or visit your site.

After a suggestion from someone on one of these lists, I came up with the
following solution which works perfectly.  Basically I append the version
number of the application to every js file reference.  For example my
application is currently on version 3.3, so every js file reference
currently looks like this:

<SCRIPT Language="javascript" SRC="file.js?3.3"></SCRIPT>

the querystring is meaningless to the js file, but as far as the browser is
concerned when the version number changes this is a new request which is not
contained in the cache.

Of course I don't hard-code this version number, I do it in ASP.  in
global.asa there is an application variable which contains the version
number, then each file reference looks like this:

<SCRIPT Language="javascript"
SRC="file.js?<%=application("version")%>"></SCRIPT>

If you don't use ASP then the same thing could be acheived in any other
server-side technology.

Alternatively if the number was hard-coded into html files, a batch script
could be written that goes through the entire site and uses a regular
epression to identify js file references and replace the version number.


-----Original Message-----
From: Chris Thompson [mailto:mail@t...]
Sent: 19 December 2001 17:00
To: JavaScript HowTo
Subject: [javascript_howto] Caching


Is there a way or some JavaScript code I can add to a page so it is never
cached, or basically reloaded everytime a user goes to that page (at least
every time a new browser is opened and goes there).  Is there a way to have
it check itself for a newer version.  I know you can have your IE settings
do that, but I was wondering if there was a way in code to do it for me.
I'd like the graphics to get cached and I do not mind if the pages do if I
can somehow force it to refresh after the browser is closed and a new one
opened.  It is a graphic intensive page (hence wanting graphics cached), but
the text changes often.  Any ideas?



$subst('Email.Unsub').

________________________________________________________________________
Scottish Enterprise Network
http://www.scottish-enterprise.com

Headquarters Address & Contact Numbers

150 Broomielaw
5 Atlantic Quay
Glasgow
G2 8LU.
Tel:  +44 (0) 141 248 2700.
Fax:  +44 (0)141 221 3217

 This message is sent in confidence for the addressee only.
It may contain legally privileged information. The contents are not to
be disclosed to anyone other than the addressee. Unauthorised recipients
are requested to preserve this confidentiality and to advise the sender
immediately of any error in transmission.



  Return to Index