FAQ Topic - How do I force a reload from the server/prevent caching?

F

FAQ server

-----------------------------------------------------------------------
FAQ Topic - How do I force a reload from the server/prevent caching?
-----------------------------------------------------------------------

To reload a page, use location.reload(). However, this depends
upon the cache headers that your server sends. To change this,
you need to alter the server configuration. A quick fix on the
client is to change the page URI so that it contains a unique
element, such as the current time. For example:
« location.replace(location.href+'?d='+new Date().valueOf()) »
If the location.href already contains a Query String, use:
« location.replace(location.href+'&d='+new Date().valueOf()) »

http://www.mnot.net/cache_docs/

http://docs.sun.com/source/816-6408-10/date.htm


===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.
 
E

Evertjan.

FAQ server wrote on 27 okt 2006 in comp.lang.javascript:
-----------------------------------------------------------------------
FAQ Topic - How do I force a reload from the server/prevent caching?
-----------------------------------------------------------------------

To reload a page, use location.reload(). However, this depends
upon the cache headers that your server sends. To change this,
you need to alter the server configuration. A quick fix on the
client is to change the page URI so that it contains a unique
element, such as the current time. For example:
® location.replace(location.href+'?d='+new Date().valueOf()) ¯

new Date().valueOf().toString(36)

will give a more compact [8 against 13 chars] equally functional string,
[non repeating if the time between requests is of reasonable length.]

If the location.href already contains a Query String, use:
® location.replace(location.href+'&d='+new Date().valueOf()) ¯

This is a bad advice, since a d=.. could be already present or
it's absense to be tested.

Use:

location.replace(location.href+'&new Date().valueOf())

or as shown above:

location.replace(location.href+'&new Date().valueOf().toString(36))
 
V

vifito

Idea:
location.href = location.href + '&' + Math.random();

Evertjan. said:
FAQ server wrote on 27 okt 2006 in comp.lang.javascript:
-----------------------------------------------------------------------
FAQ Topic - How do I force a reload from the server/prevent caching?
-----------------------------------------------------------------------

To reload a page, use location.reload(). However, this depends
upon the cache headers that your server sends. To change this,
you need to alter the server configuration. A quick fix on the
client is to change the page URI so that it contains a unique
element, such as the current time. For example:
® location.replace(location.href+'?d='+new Date().valueOf()) ¯

new Date().valueOf().toString(36)

will give a more compact [8 against 13 chars] equally functional string,
[non repeating if the time between requests is of reasonable length.]

If the location.href already contains a Query String, use:
® location.replace(location.href+'&d='+new Date().valueOf()) ¯

This is a bad advice, since a d=.. could be already present or
it's absense to be tested.

Use:

location.replace(location.href+'&new Date().valueOf())

or as shown above:

location.replace(location.href+'&new Date().valueOf().toString(36))
 
R

Richard Cornford

vifito said:
Idea:
location.href = location.href + '&' + Math.random();

Two consecutive random numbers may be the same number, and numbers
certainly may re-occur within a session, or the expiry period of a cached
document. Time values have the advantage of being a non-repeating
sequence so they will all be unique if requested less frequently than the
rate at which time is incremented on OS in use.

Richard.
 
K

Kevin Darling

The option of "location.reload(true)" should perhaps be mentioned.
Without the parameter, you can get what's in the cache. The "true"
is supposed to force a real reload.

Kev
 
D

Dr J R Stockton

Fri said:
new Date().valueOf().toString(36)

will give a more compact [8 against 13 chars] equally functional
string, [non repeating if the time between requests is of reasonable
length.]

And

(+new Date()).toString(36)

gives the same with less code.


I suggest rewording "of reasonable length", since at first I took it to
mean "not too long" - and there the limit would be over 275,000 years.

AFAIK, a nominal 55 ms will always be long enough for the time to have
changed.



With the sort of algorithm which one expects to be used, Math.Random()
will repeat only after 2^32, 2^53, or 2^64 calls. Conversions to String
*may* repeat more often on the average, and possibly sometimes quite
soon. It is probably initialised from the clock, either per-page or per
browser instance. That gets complex.

<URL:http://www.merlyn.demon.co.uk/js-randm.htm#MR> should show the
resolution of Math.random(); but think about the code.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top