Method 1: Using the
window.location.reload() method. This method can be called without any arguments, and it will refresh the current page:
window.location.reload() method also accepts an optional argument: a boolean value that, when set to
true, forces the browser to perform a hard reload, bypassing the cache. By default, this value is set to
false, meaning the browser will perform a soft reload using the cached resources:
window.location.reload(false); // Soft reload (default)
Method 2: Setting the
window.location.href property to its current value. This action tells the browser to navigate to the same URL, which effectively refreshes the page:
Method 3: Using the
Finally, you can use the
location.replace() method to refresh a page. This method replaces the current document with the specified URL, and in our case, we’ll replace it with the same URL:
It is important to note that using
location.replace() will not add a new entry to the browser’s history. This means that if the user clicks the back button after the page has been refreshed, they will be taken to the page they visited before the current one, rather than the refreshed version of the current page.
window.location.reload() method, setting the
window.location.href property, and using the
location.replace() method. Choose the method that best fits your use case, and happy coding!