Last updated on June 6, 2022
To refresh or reload the active or current pages using JavaScript, you can use the location.reload() ,and setTimeout()
methods.
The reload() method does the same as the reload button in your browser.
setTimeout() method calls to evaluate an expression after a specified number of milliseconds.
By default, the reload()
method reloads the page from the cache, but you can force it to reload the page from the server by setting the forceGet
parameter to true
: location.reload(true)
.
Example script to Auto-refresh current page
Source code of a sample HTML page that reloads the page with 3 seconds regular intervals.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<h1>Hello, world!</h1>
<script>
setTimeout(function(){
location.reload();
},3000); // 3000 milliseconds means 3 seconds.
</script>
</body>
</html>