Cara menggunakan javascript hard refresh

Pemrograman, desain, pengetesan, menanggapi respon pengguna dan menulis semua bahan untuk website ini dikerjakan oleh Toni Podmanicki, Paul Irish dan Jeremy Hill. Kami ingin berterima kasih kepada semua orang yang telah membantu dan memberikan kontribusi dalam bentuk apapun. Semoga halaman ini bermanfaat bagi anda.

We had this problem when we removed our style sheets and put them into a HttpHandler to serve them all in one single request. The problem was that due to some caching problem, users' needed to refresh their page to receive the updated CSS files. This resulted in confused users looking at a non-styled site featuring trendy black Times New Roman font on a original white background with blue and purple retro links? old skool...

We (as developers) nonchalantly refreshed the page and happily continued, others didn't and we quickly realised that "Oh, they just need to refresh the page" wasn't good enough.

Solution

We needed a way to make the users refresh their browser caches, but how since we have no control over their browsers?

I came up with a bit of JavaScript that will do a hard refresh of the page and use cookies to record that it has been refresh and ensure that it only happens once and doesn't go into an infinite loop.

Here is some code I came up with:
//jquery plugin for cookies
<script type="text/javascript" src="/js/jquery/jquery.cookies.2.0.1.min.js"></script>
<script type="text/javascript">
//give it a new name each time you need to do this
var cookieName = 'refreshv1';
//check client can use cookies
if ($.cookies.test()) {
    //get the cookie
    var c = $.cookies.get(cookieName);
    //if it doesn't exist this is their first time and they need the refresh
    if (c == null) {
        //set cookie so this happens only once
        $.cookies.set(cookieName, true, { expires: 7 });
        //do a "hard refresh" of the page, clearing the cache
        location.reload(true);
    }
}
</script>
Feel free to work with this code but please do not use it without testing or ensuring it is right for your situation. It is merely an idea that worked for me.

PHP caches data for some functions for better performance. If a file is to be checked several times in a script, you probably want to avoid caching to get correct results. To do this, use the clearstatcache() function.

When you're developing applications like a blog or a page where the data may change based on user actions, you'll want that page to refresh frequently.

When the page refreshes or reloads, it will show any new data based off those user interactions. Good news – you can implement this type of functionality in JavaScript with a single line of code.

In this article, we will learn how to reload a webpage in JavaScript, as well as see some other situations where we might want to implement these reloads and how to do so.

Here's an Interactive Scrim about How to Refesh the Page in JavaScript

How to Refresh a Page in JavaScript With location.reload()

You can use the location.reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button.

The reload() method is the main method responsible for page reloading. On the other hand, location is an interface that represents the actual location (URL) of the object it is linked to – in this case the URL of the page we want to reload. It can be accessed via either document.location or

<button type="button" onClick="window.location.reload()">
   Reload Page
</button>
0.

The following is the syntax for reloading a page:

window.location.reload();

Note: When you read through some resources on “page reload in JavaScript”, you'll come across various explanations stating that the relaod method takes in boolean values as parameters and that the

<button type="button" onClick="window.location.reload()">
   Reload Page
</button>
1 helps force-reload so as to bypass its cache. But this isn't the case.

According to the MDN Documentation, a boolean parameter is not part of the current specification for location.reload() — and in fact has never been part of any specification for location.reload() ever published.

Browsers such as Firefox, on the other hand, support the use of a non-standard boolean parameter known as

<button type="button" onClick="window.location.reload()">
   Reload Page
</button>
4 for location.reload(), which instructs Firefox to bypass its cache and force-reload the current document.

Aside from Firefox, any parameters you specify in a location.reload() call in other browsers will be ignored and have no effect.

How to Perform Page Reload/Refresh in JavaScript When a Button is Clicked

So far we have seen how reload works in JavaScript. Now let’s now see how you can implement this could when an event occurs or when an action like a button click occurs:

<button type="button" onClick="window.location.reload()">
   Reload Page
</button>

Note: This works similarly to when we use

<button type="button" onClick="window.location.reload()">
   Reload Page
</button>
7.

How to Refresh/Reload a Page Automatically in JavaScript

We can also allow a page refersh after a fixed time use the

<button type="button" onClick="window.location.reload()">
   Reload Page
</button>
8 method as seen below:

setTimeout(() => {
  document.location.reload();
}, 3000);

Using the code above our web page will reload every 3 seconds.

So far, we've seen how to use the reload method in our HTML file when we attach it to specific events, as well as in our JavaScript file.

How to Refresh/Reload a Page Using the History Function in JavaScript

The History function is another method for refreshing a page. The history function is used as usual to navigate back or forward by passing in either a positive or negative value.

For example, if we want to go back in time, we will use:

history.go(-1);

This will load the page and take us to the previous page we navigated to. But if we only want to refresh the current page, we can do so by not passing any parameters or by passing

<button type="button" onClick="window.location.reload()">
   Reload Page
</button>
9 (a neutral value):

history.go();
history.go(0);

Note: This also works the same way as we added the reload() method to the

<button type="button" onClick="window.location.reload()">
   Reload Page
</button>
8 method and the click event in HTML.

Wrapping Up

In this article, we learned how to refresh a page using JavaScript. We also clarified a common misconception that leads to people passing boolean parameters into the reload() method.

Thanks for reading!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


Cara menggunakan javascript hard refresh
Joel Olawanle

Frontend Developer & Technical Writer


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started