Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (6.1k points)
Can anybody tell me how can I refresh a page using javascript or HTML?

1 Answer

0 votes
by (11.7k points)
edited by

There are few methods to refresh a page in javascript.

The location.reload() method reloads the current web page in a web browser. 

The method gives the user the appropriate same result as pressing the RELOAD button in your browser.

The JavaScript reload page method loads the page from the cache by default. If the forceGet property is set to true, the page is reloaded from the server. The JS reload page method does not have a return value.

Using location.reload()

Refresh page function in Javascript can be reloaded in  the current resource. In common cases, a page is selected to be refreshed. The method also helps in a few other things, such as helping you get the URL address of the current page, redirect the browser to another page, and, of course, refresh page JavaScript.

Example:

HTML code:

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="scripts.js"></script>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<p>location.reload function</p>

<button onclick="sampleFunction()">Reload the page!</button>

</body>

</html>

CSS code:

body {

 background-color: aquamarine;

}

Javascript Code:

(() => {

 setTimeout(() => {

 document.getElementsByTagName("body")[0].style.backgroundColor = 'teal';

 }, 50)

})();

function sampleFunction() {

location.reload();

}

location.reload() Syntax:

In this way user can call the refresh page JavaScript method from within your code (to reload from server):

location.reload(forceGet)

It’s indicated that JavaScript refresh page command has the forceGet parameter added in the parentheses. This parameter has a boolean value of either true or false. But, it is optional.

The forceGet parameter should be only used when developers want to force the website to reload from the server. The JavaScript refresh page function without a parameter in its parentheses will reload the page from cache.

HTML CODE:

<!DOCTYPE html>

<html>

<head>

  <script type="text/javascript" src="scripts.js">

  </script>

  <link rel="stylesheet" href="styles.css">

</head>

<body>

  <p>location.reload function</p>

  <button onclick="sampleFunction()">Reload the page!</button>

</body>

</html>

CSS Code:

body {

 background-color: aquamarine;

}

Javascript Code:

(() => {

 setTimeout(() => {

   document.getElementsByTagName("body")[0].style.backgroundColor = 'lightgreen';

 }, 50)

})();

function sampleFunction() {

 location.reload(true);

}

Want to become an expert in web technology, check out these Web Development Courses provided by Intellipaat!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...