The Revolutionary Web

Henry’s dev notes about the web

JavaScript: How To Force Page To Reload

When you want to load a page on a , you would normally click the / button or press F5 or CTRL-R on your keyboard. If you set your to cache previously viewed pages, doing a normal page will cause the to fetch the requested page from cache unless otherwise instructed by a script or meta tag in the page. When this happens, images and css will be read from cache and form fields will also retain their values (this included text, textarea, radio button and checkbox).

In order to completely force a page to , which means, instructing the to skip the cached data and re-fetch everything from the server, you need to press CTRL-F5 on IE and Firefox or Command-R on Mac-Safari.

In , there are also different ways to a page. Here are the methods for a normal page :

window.location.assign(url);
Loads the page at the the provided URL.

window.location.replace(url);
Same as assign() method except that when replace() is used, the current page will not be saved in the session history as it was replaced by the provided URL.

document.location = url;
Technically, location is a read-only property of the document. But some browsers allow you to assign a URL to it, which causes the page to . The reason for this is because whenever the location property is modified, a page will with the new assigned URL. I recommend using window.location for compatibility.

window.location.href = url;
Same as document.location

The other properties of location that when modified will the page are hash, host, hostname, pathname, port, protocol and search.

What if you want to completely force a page to ? Say, you want to clear all entered values in a form or maybe you have modified your CSS and you want to make sure that the page will pickup the new changes. In order to do so, you can do the following:

window.location.(true);

The method accepts a boolean value, which, when it is true, causes the page to always fetch document from the server. When none is specified, it defaults to false, which may the page from its cache.

As you can see, the methods for forcing a page to has their own purposes. If you simply want to the page and do not care about displaying fresh data, you should not always force it to from the server to avoid making http requests, which is an expensive call and a waste of bandwidth. You are not only calling the page itself but you maybe reloading an external CSS page too.

8 Responses to “JavaScript: How To Force Page To Reload”

  1. tony Says:

    Thanks for the information. However In my experience “window.location.reload(true);” does not force Safari to reload.

  2. raptor Says:

    Yes. Safari does not work on “window.location.reload(true)”. Tried on Safari 1-3.

  3. SB Dude Says:

    For Safari and others I use:
    window.location.href = window.location.href

    If reloads the page with new data

  4. Shoman Says:

    Guys,
    Is it possible to create links on the values those are provided by dynamic populating the drop down list based on the selected value of first list?

    Consider two drop down menus.
    one works with category and the other one on subcategories.

    CATEGORY: FRUIT, SPORTS
    if FRUIT is chosen as category the
    SUBCATEGORY will contain Mango, Apple and so on

    My question is how to make links for the subcategory values for the above example?

    Clicking on mango will lead to mango.php and for apple to apple.php.

    Thanks.
    shoman

  5. Keena Says:

    Great work.

  6. PREENTENVERTYTINK Says:

    hello it is test. WinRAR provides the full RAR and ZIP file support, can decompress CAB, GZIP, ACE and other archive formats.
    pfbegpaisxnygrihpfkvkemqzaxeuqrudvahello

  7. Rudie Says:

    If you want to reload the page in safari and still want to go to the included hash (for instance an anchor on that page, or a certain tab like in my case) you can just alter the url a bit. Just add a bogus parameter which is empty and it will work. I just use it one time, so it’s not that bad. I can live with it.

  8. Renzo Says:

    You don’t have any idea of how long i’ve been looking for a method to avoid the BACK action from browsers. Today I’ve seen the light at the end of the tunnel with location.replace. Thank you so much!

Leave a Reply