| by Arround The Web | No comments

How to Use Location reload() Method in JavaScript

The refresh or reload process plays an important role to ensure that the content available on the browser is accurate and up to date. It is generally used to sync out the web page content that is stored in the browser cache. It does not affect the structure of the web page or site however, only updates the newly made changes. Considering its importance, JavaScript provides the “location.reload()” method to refresh/reload a web page conveniently.

This guide illustrates the working and usage of the JavaScript “location.reload()” method.

How to Use the JavaScript Location “reload()” Method?

The “location.reload()” method refreshes the web page URL from the cache by default. It works as a “refresh” and “reload” button of the browser.

Syntax

location.reload()

 
Parameters

The “location.reload()” supports two optional parameters that are stated below:

location.reload(forceget)

 
The above syntax comprises the boolean values i.e., “true” or “false”:

    • false: This boolean type sets by default that refresh the URL from the cache.
    • true: It represents the boolean type that reloads the URL from the server.

Example: Applying the Location “reload()” Method to Reload a Web Page

In this example, the “location.reload()” method is utilized to reload the current web page.

HTML Code

Firstly, overview of the following HTML code:

<h2>Use Location reload() Method in JavaScript</h2>
<p>Click on the button to reload the current web page</p>
<button ondblclick = "real()">Reload page</button>

 
In the above code snippet:

    • The “<h2>” tag creates a subheading.
    • In the next step, the “<p>” tag creates a paragraph with the defined statement.
    • After that, create a button having an associated “ondblclick” event redirecting to the function named “real()” that will be triggered upon the button double-click.

JavaScript Code

Now, have a look at the below-stated code:

<script>
function real() {
 location.reload();    
}
</script>

 
In the above code, the defined “real()” function uses the “location.reload()” method to reload the current web page from the cache.

Output


As seen, clicking the “Reload page” button reloads the current web page URL accordingly.

Conclusion

JavaScript offers the “location.reload()” method that works on the built-in “location” object for reloading the current URL. It refreshes/reloads the content from the cache, not from the server. However, it supports the additional “forceGet” parameter having the boolean type “true” to reload the web page from the server. This guide illustrated the working and use of the Location “reload()” method in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply