/    Sign up×
Community /Pin to ProfileBookmark

automatically retrieve from server . no cache

Hi,

I have a couple of pages on my site that when they are viewed i always want them to be retrieved from the server and not from the browsers cache.

There must be a way to do this. Does anyone have a piece of javascript code that does this? Or even a piece of code that looks at the date of a cached file then reloads/refreshes the page if the date stamp is say, too old!

I know there is an html meta tag where you can refresh a page after so many seconds but i dont want to do that. I want to grab the page from the server every time the page is viewed.

I hope someone can help. I’ve been stumped on this problem for months!

Thanks

Andy

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 01.2006 — There is a meta tag for bypssing cache, too. But this varies from browser to browser and then there is still the possible problem of server cache. The JavaScript method for accomplishing this is as follows:

<a href="...url..." onclick="this.href += ((this.href.indexOf('?') < 0) ? '?' : '&') + (new Date()).getTime(); return true;">link text</a>
Copy linkTweet thisAlerts:
@mrhooApr 01.2006 — php didn't tell you, Andy, that the technical javascript name for this method of loading an apparently fresh page is 'pin the tail on the URL'.
Copy linkTweet thisAlerts:
@andy_bauthorApr 04.2006 — Thanks guys,

but will this mean the user will have to physically click the link "link text" doesnt' it? Isnt this equivalent of hitting the reload/refresh button in the browser toolbar?

Andy
Copy linkTweet thisAlerts:
@phpnoviceApr 04.2006 — I showed a link -- but the same concept can be applied to any server request via url. If you just want to refresh the current page, then the following is equivalent to clicking the browser's reload/refresh button:

top.location.reload(true)
Copy linkTweet thisAlerts:
@andy_bauthorApr 10.2006 — so i tried adding top.location.reload(true); to my webpage.

However when this page is reloaded (pressing the reload/ refresh button) in firefox the page seems to keep reloading without ever displaying the content. It seems to be equivalent to continuously pressing the reload button.

Is this the expected behaviour?

All i am trying to do it to have the page reloaded once as soon as it is visited. i.e. never use cache.

thanks
Copy linkTweet thisAlerts:
@felgallApr 10.2006 — Sounds like you have told the page to reload every time it starts to load which therefore puts it into an infinite loop. What you need to do is use a cookie to keep track of whether the page has already loaded and only reload if it hsn't loaded before.
Copy linkTweet thisAlerts:
@andy_bauthorApr 11.2006 — Thanks Stephen

But thats not i want to do.

I want to ALWAYS retrieve data from the server every time a page is viewed. Never use the cached page. I only want to do this for 2 pages on my site as they contain data that will be updated frequently.

How do companies do this that update their web content hourly?

thanks

but yes with the previous set up (top.location.reload(true)) it was essentially entering an infinite loop
Copy linkTweet thisAlerts:
@phpnoviceApr 11.2006 — How do companies do this that update their web content hourly?[/QUOTE]
There are two basic categories of META tags for such things. One will expire your page immediately upon delivery. The other instructs the browser not to cache the page. Some work in some browsers at some release levels. Others work in other browsers. Some browsers ignore both. You'll have to do the research. The only other possibility is using server-side code to send particular HTTP headers that also instruct the browser in some way. Again, you will have to do the research.
Copy linkTweet thisAlerts:
@balloonbuffoonApr 11.2006 — Put this in your head:
[code=html]<script>
if(location.search.indexOf('nocache=')<0) {
location.href+=((location.href.indexOf('?') < 0) ? '?' : '&') + "nocache=" + (new Date()).getTime();
}
</script>[/code]

That [I]should[/I] work.

--Steve
×

Success!

Help @andy_b spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...