To refresh a page using JavaScript, you can use the location.reload
method of the window.location
object. The location.reload
method reloads the current page from the server, and it has an optional argument that specifies whether to reload the page from the cache or from the server.
Here is an example of how to use the location.reload
method to refresh a page:
// Reload the page from the server
location.reload();
// Reload the page from the cache
location.reload(true);
The location.reload
method is often used in conjunction with the confirm
function to prompt the user to confirm the reload before it is executed. For example:
if (confirm('Are you sure you want to refresh the page?')) {
location.reload();
}
Note that the location.reload
method only works for the current page, and it cannot be used to refresh a different page or to load a new page. If you want to refresh a different page or load a new page, you can use the location.assign
method or the location.replace
method.