How to refresh a page with jQuery
Topic: JavaScript / jQueryPrev|Next
Answer: Use the JavaScript location.reload()
Method
You can simply use the JavaScript location.reload()
method to refresh or reloads the page. This method optionally accepts a Boolean parameter true
or false
.
If the parameter true
is specified, it causes the page to always be reloaded from the server. But, if it is false
(which is default) or not specified, the browser may reload the page from its cache.
Let's check out an example to understand how this method basically works:
Example
Try this code »<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Reload the Page Using jQuery</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
location.reload(true);
});
});
</script>
</head>
<body>
<button type="button">Reload page</button>
</body>
</html>
You can also do the same thing with plain JavaScript. You don't need jQuery for this. Check out the following example which is the modified version of the previous example.
Example
Try this code »<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Refresh the Page with JavaScript</title>
<script>
function reloadPage(){
location.reload(true);
}
</script>
</head>
<body>
<button type="button" onclick="reloadPage();">Reload page</button>
</body>
</html>
Related FAQ
Here are some more FAQ related to this topic: