How can I reload the page using JavaScript?
I need a method that works in all browsers.
You can perform this task using window.location.reload();
. As there are many ways to do this but I think it is the appropriate way to reload the same document with JavaScript. Here is the explanation
JavaScript window.location
object can be used
window
: in JavaScript represents an open window in a browser.
location
: in JavaScript holds information about current URL.
The location
object is like a fragment of the window
object and is called up through the window.location
property.
location
object has three methods:
assign()
: used to load a new documentreload()
: used to reload current documentreplace()
: used to replace current document with a new oneSo here we need to use reload()
, because it can help us in reloading the same document.
So use it like window.location.reload();
.
Online demo on jsfiddle
To ask your browser to retrieve the page directly from the server not from the cache, you can pass a true
parameter to location.reload()
. This method is compatible with all major browsers, including IE, Chrome, Firefox, Safari, Opera.