470,614 Members | 1,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

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

-----------------------------------------------------------------------
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.

Oct 26 '06 #1
5 2380
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))

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 27 '06 #2
Idea:
location.href = location.href + '&' + Math.random();

Evertjan. wrote:
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))

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 27 '06 #3
vifito wrote:
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.
Oct 27 '06 #4

FAQ server wrote:
-----------------------------------------------------------------------
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.
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

Oct 27 '06 #5
In message <Xn********************@194.109.133.242>, Fri, 27 Oct 2006
08:05:08, Evertjan. <ex**************@interxnl.netwrites
>
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#MRshould show the
resolution of Math.random(); but think about the code.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Martin | last post: by
3 posts views Thread by ghostwolf | last post: by
1 post views Thread by Rob | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.