Refresh Javascript Sources

J

JamesBond

I am using a javascript in my html page which will be updated daily.
However, the javascript is getting cached by the browser and whenever
the user visits my html page, he is getting a cached copy of the
javascript though the javascript is updated.

Is there any way to force the browser not to cache this javascript?
 
R

Richard Cornford

JamesBond said:
I am using a javascript in my html page which will be
updated daily. However, the javascript is getting cached
by the browser and whenever the user visits my html page,
he is getting a cached copy of the javascript though the
javascript is updated.

Is there any way to force the browser not to cache this
javascript?

That would be silly. You want the javascript cached as much as possible.
(You want everything cached as much as possible). If you are changing
the script on a daily bases you should send an HTTP Expires header than
tells the browser when the current version expires and then it should
check for a new version when the old one expires.

Alternatively you could put a (dummy) query string in the SRC attribute
for the javascript file and change that whenever you want a new version
recovered from the server. I.E. you might start with:-

SRC="someJsFile.js?V=1"

- and when you update the JS file change the references to:-

SRC="someJsFile.js?V=2"

- and the browser will not see its cached version as the resource in
question and it will have to get a new one from the server. That way the
file is still cached on the client whenever it is not changed.

Obviously going through an entire web site updating query stings is not
a good idea so this would be better done dynamically, with the version
number that is appended to the query string available as an application
wide variable. This works best for intermittent and unpredictable
updates, if you are genuinely updating daily then appropriate Expires
headers are a much better approach.

Richard.
 
J

JamesBond

Hi Richard,

Thanks for your reply. But, could you please let me know how I could
sent Expires header in a javascript. I am very new to this.

Thanks,
James.
 
T

Thomas 'PointedEars' Lahn

JamesBond said:
Thanks for your reply. But, could you please let me know how I could
sent Expires header in a javascript. I am very new to this.

That header can only be set/sent from server-side, for example through
JScript in ASP. Googling for "iis response header expires" linked to
<URL:http://support.microsoft.com/kb/q234067/> as first hit.


PointedEars
 
J

JamesBond

Is it possible that I call a php page which returns javascript content
from the javascript tag as shown in the example below?
<script language="javascript" src="/js.php"></script>

In such a case, will the browser still cache it??
 
T

Thomas 'PointedEars' Lahn

JamesBond said:
Is it possible that I call a php page which returns javascript
content from the javascript tag as shown in the example below?
<script language="javascript" src="/js.php"></script>

Yes, however that should be

<script type="text/javascript" src="/js.php"></script>

See also
In such a case, will the browser still cache it??

See <URL:http://www.mnot.net/cache_docs/>.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top