JavaScript is full of useful functions, location.reload()
is one of them. This function refreshes the whole webpage. Similarly, JavaScript location.reload(true) hard-refreshes the webpage. But we can always reload the webpage by pressing F5
or by using the browser's reload button. So the questions that arise are, What use location.reload()?, and what is hard refresh? Let's find out the answer!
The location.reload()
method refreshes the current webpage from the browser's cache. Browser cache is temporary internet files that stores website's images, document, and data for faster future reloads. location.reload()
use this cache data to reload the webpages.
The Location reloads method can be invoked by a button click or by any other event. The most popular use is to automatically reload the webpage. This can be achieved by using setTimeout()
with location.reload()
. Look at the example below.
Automatic refresh works best for websites that need frequent reloads like news websites, sports websites that show live scores
, etc. But these websites need updated data, right from the server. So, we have to tweak the function a little bit to start refreshing the page from the server(Next topic).
Example of location.reload() with button:
location.reload function
Example of Automatic reloading of the webpage using setTimeout() method:
JavaScript location.reload(true)
, can you spot the difference? Yes, a parameter is passed in the function, 'true
'. This is how we refresh the webpage from the server, it's that easy. It is a boolean parameter, so, it has only two values true and false.
By default, location.reload()
has 'false
' passed as a parameter. This parameter is for hard refresh from the server. When false
gets passed, the webpage reloads from the browser's cache. However, when true
gets passed, the browser requests server for a fresh copy of the webpage.
location.reload(false)
.
The Javascript reload() method is used to reload the current document or URL. The javascript location.reload(true) method work just like reload button in your browser. By default, the JS reload() method reloads the page from the cache, however you may force it to reload the page from the server side by setting the forceGet parameter to true: location. reload(true).
We use location.reload() to reload web document and it is the valid syntax. It is only the reload with forcedReload which is now deprecated.
To Solve location.reload(true) is deprecated Error we should use location.reload() without the forceReload flag.
You can also check JavaScript Window Location that contains the information about the current URL.
Follow Us: