Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force window.location to make an HTTP request instead of using the cache?

In my web application I am setting window.location to navigate to a different page, but for some reason Firefox shows an old version of that page.

Using Firebug I detected that the browser doesn't even send an HTTP request, it simply uses an older version of that page (not even the last one) and displays it.

The page itself has all the usual headers to prevent caching which works perfectly when I browse my pages using links or manual input. The problem only occurs when seting window.location.

Is this a Firefox problem or something to expect from any browser? Can this behaviour be changed?

like image 375
Daniel Rikowski Avatar asked Jan 07 '13 13:01

Daniel Rikowski


People also ask

How do I force a website to not cache?

When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache. You can then close out of Developer Tools.

Which HTTP request will never get cached?

A proxy HTTP server that forwards your message to the server would never cache anything but a GET or a HEAD request.

What is the difference between location reload () and window location reload ()?

location. reload() takes an additional argument skipCache so that with using window. location. reload(true) the browser will skip the cache and reload the page from the server.

Are HTTP requests cached?

All HTTP requests that the browser makes are first routed to the browser cache to check whether there is a valid cached response that can be used to fulfill the request. If there's a match, the response is read from the cache, which eliminates both the network latency and the data costs that the transfer incurs.


2 Answers

You could just add a random parameter to the page URL in order to have the browser issue a new request.

So instead of using

 window.location = "my.url/index.html";

use

 window.location = "my.url/index.html?nocache=" + (new Date()).getTime();
like image 124
Sirko Avatar answered Oct 31 '22 00:10

Sirko


You can use location.reload with a true argument, which will always bypass the cache.

window.location.reload(true);
like image 30
Nick Bray Avatar answered Oct 31 '22 01:10

Nick Bray