How to Reload a Page using JavaScript

JavaScript provides a trendy among developers — location.reload() , which finds ways to reload the page at the current URL.

The window.location object can be used for getting the current page’s URL, redirecting the browser to another page, and reloading the same page. The window represents an open window in a browser, and location holds information about the current URL.

The location.reload has the forceGet parameter. The parameter has a boolean value of either true or false.

To reload the same page, do the following:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <input type="button" value="Reload Page" onClick="window.location.reload(true)">
    <div>Welcome to W3Docs</div>
  </body>
</html>

If the parameter is set to true, it will reload the page from the server.

If the parameter is set to false it will reload the page using the version of the page cached by the browser:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <input type="button" value="Reload Page" onClick="window.location.reload(false)">
    <div>Welcome to W3Docs</div>
  </body>
</html>
The JavaScript reload() method loads the page from the cache, by default.

False is the default parameter. If you don’t specify, it will result in the same way as window.location.reload(false);

There is also another way to reload the page using the timeRefresh command. You can specify how frequently to refresh the page, and it will be loaded non-stop:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body onLoad="timeRefresh(2000);">
    <script>
      function timeRefresh(time) {
        setTimeout("location.reload(true);", time);
      }
    </script>
    <h3>This page will auto refresh after 2 seconds.</h3>
  </body>
</html>

The Location.reload() Method

The Location interface represents the URL of the object to which it is linked. The location object is like a piece of the window object and is called through the window.location property. One of the location’s methods is reload(), which is used to reload a document.