JavaScript location reload true

JavaScript provides a method to refresh or reload a web page by location.reload() function. It accepts one parameter, which contains a boolean value of either true as location.reload(true) or false as location.reload(false). It is typically invoked on the window.location object, which represents the current URL of the web page. Syntax of the function is given below.

location.reload(forceGet)

The forceGet parameter is an optional boolean value that determines the execution of the page reload process.

When forceGet is true, it forces the page to be reload from the server, bypassing the browser cache.

window.location.reload(true);

In contrast, when forceGet is false, the page will reload using cached resources whenever possible.

window.location.reload();
// OR
window.location.reload(false);

when the reload() function called with no arguments, the browser tries to reuse cached resources, reducing the number of HTTP requests to the server.

Example to refresh or reload a web page by location.reload(true) function

In this example we have created a HTML button with an onclick event to call a function which defines how to simply reload or refresh the page in JavaScript.

when we click on “Force Refresh Page” button, the browser will attempt to reload the page from the server bypassing the browser cache.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript location.reload(true)</title>
</head>
<body>

    <button onclick="forceRefreshPage()">Force Refresh Page</button>

    <script>
        const forceRefreshPage = () => {
            try {
                location.reload(true); // Attempt to force reload bypassing cache (deprecated)
            } catch (error) {
                console.error("Failed to force reload:", error);
                location.reload(); // Fallback to normal reload
            }
        }
    </script>

</body>
</html>

The above example shows how to use location.reload() function with an onclick event but if you want to automatically reload or refresh the page then use this function with the setTimeout() function.

Following are the browsers that supports location.reload(true) function.

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 5.5
  • Firefox 1
  • Opera 3
  • Safari 1

Read more – How to clear Browser Cache