Get in Touch With Us
Submitting the form below will ensure a prompt response from us.
Refreshing a web page is one of the simplest actions you can perform using JavaScript, but it plays a critical role in many interactive web applications. One of the commonly used commands for this is location. reload(true). Although straightforward, this method has nuances that every developer should understand, especially since the behavior of true has changed over time.
In this guide, we’ll dive into what Javascript:Location.Reload(True) does, how it works, when to use it, and what has changed in modern browsers.
What is Javascript:Location.Reload(True) in JS?
Location.reload() is a JavaScript method that reloads the current URL, just like refreshing the page using the browser’s refresh button (F5).
Basic Syntax:
location.reload();
This reloads the page from the browser cache by default.
What Does location.reload(true) Mean?
In older versions of JavaScript and some legacy browsers, location. reload(true) was used to force a reload from the server rather than from the browser cache.
Example:
location.reload(true); // forces page reload from server (deprecated)
However, the true argument is now deprecated and has no effect in modern browsers like Chrome, Firefox, and Edge. Today, whether the page reloads from the server or cache is controlled by the browser itself.
Current Usage: Just Use location.reload()
If you’re writing modern JavaScript, the recommended usage is simply:
location.reload();
This works well in almost all cases unless you’re specifically handling cache issues.
Forcing a Fresh Reload (Workaround)
If you want to ensure that the page loads fresh (as location.reload(true) once did), a better approach today is to modify the URL with a cache-busting query string:
window.location.href = window.location.href.split('?')[0] + '?cache=' + new Date().getTime();
Or redirect to the same page using:
window.location.replace(window.location.pathname + '?refresh=' + new Date().getTime());
This tricks the browser into thinking it’s a new page, thereby bypassing the cache.
Common Use Cases
After Form Submission
To reload the page and show updated data:
document.getElementById("submitBtn").onclick = function () {
// Submit logic here...
location.reload();
};
After Logout
To clear user sessions and redirect:
function logoutUser() {
// Perform logout
sessionStorage.clear();
location.reload();
}
Refreshing a SPA or Dashboard
Used in interval-based data refresh scenarios:
setInterval(() => {
location.reload();
}, 60000); // reloads every 60 seconds
Deprecation Warning: location.reload(true)
Modern browsers ignore the true parameter, and some may even warn you in the console:
Warning: location.reload(true) is deprecated and ignored.
So, avoid using it in production code. Use cache-busting or service worker techniques instead for more robust solutions.
Pro Tip: Use DevTools to Disable Cache
If you’re testing and need to force a fresh reload:
- Open Chrome DevTools (F12)
- Check “Disable Cache” in the Network tab
- Reload using location.reload()
This ensures the page reloads from the server while you’re developing.
Alternative: window.location.hrlef
If you want more control, you can also reload or redirect using:
window.location.href = window.location.href;
Or redirect to a specific route:
window.location.href = '/dashboard';
This is a more flexible way to navigate or reload with parameters.
You Might Also FAcing This Issue:
Conclusion
location.reload(true) was once used to force a server reload, but in modern JavaScript, this usage is deprecated. The correct and safe method today is simply location.reload(). If you want to bypass the cache, use a URL-based trick or control caching via HTTP headers or service workers.
Understanding this subtle difference helps write more reliable and modern JavaScript for real-world applications.
Get in Touch With Us
Submitting the form below will ensure a prompt response from us.