How can I refresh a page with jQuery?
Three approaches with different cache-related behaviours:
location.reload(true)
In browsers that implement the forcedReload
parameter of location.reload()
, reloads by fetching a fresh copy of the page and all of its resources (scripts, stylesheets, images, etc.). Will not serve any resources from the cache - gets fresh copies from the server without sending any if-modified-since
or if-none-match
headers in the request.
Equivalent to the user doing a "hard reload" in browsers where that's possible.
Note that passing true
to location.reload()
is supported in Firefox (see MDN) and Internet Explorer (see MSDN) but is not supported universally and is not part of the W3 HTML 5 spec, nor the W3 draft HTML 5.1 spec, nor the WHATWG HTML Living Standard.
In unsupporting browsers, like Google Chrome, location.reload(true)
behaves the same as location.reload()
.
location.reload()
or location.reload(false)
Reloads the page, fetching a fresh, non-cached copy of the page HTML itself, and performing RFC 7234 revalidation requests for any resources (like scripts) that the browser has cached, even if they are fresh are RFC 7234 permits the browser to serve them without revalidation.
Exactly how the browser should utilise its cache when performing a location.reload()
call isn't specified or documented as far as I can tell; I determined the behaviour above by experimentation.
This is equivalent to the user simply pressing the "refresh" button in their browser.
location = location
(or infinitely many other possible techniques that involve assigning to location
or to its properties)Only works if the page's URL doesn't contain a fragid/hashbang!
Reloads the page without refetching or revalidating any fresh resources from the cache. If the page's HTML itself is fresh, this will reload the page without performing any HTTP requests at all.
This is equivalent (from a caching perspective) to the user opening the page in a new tab.
However, if the page's URL contains a hash, this will have no effect.
Again, the caching behaviour here is unspecified as far as I know; I determined it by testing.
So, in summary, you want to use:
location = location
for maximum use of the cache, as long as the page doesn't have a hash in its URL, in which case this won't worklocation.reload(true)
to fetch new copies of all resources without revalidating (although it's not universally supported and will behave no differently to location.reload()
in some browsers, like Chrome)location.reload()
to faithfully reproduce the effect of the user clicking the 'refresh' button.