4

Javascript provides the location.reload(nocache) API.

When the 'nocache' param is true, it will force reload the current url from server bypassing the browser cache.

Is there an equivalent way to do this when navigating to a new url via window.location.href = url; ?

I have a chat application which detects the version the client is running compared to what the server expects, and if they differ it prompts the client to navigate to the latest version url.

But I'm finding when I issue this, many clients are still using cached scripts.

1 Answer 1

8

There's no out of the box solution. The easiest way is to add another parameter to the URL that will break the cache. For example

 window.location = "/server/url?timestamp=" + (new Date()).getTime()

Every single time you run it, the timestamp will change and the browser won't have that page in the cache.

Sign up to request clarification or add additional context in comments.

2 Comments

not sure if it's up to full url or js/css file url, probably op wants to disable cache for js/css files
Thanks, nice and simple. I actually already use this technique elsewhere, so I'm ashamed I hadn't though of it :) I had been trying to handle it via webserver cache headers, but it wasn't ideal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.