JavaScript

Javascript Refresh Page

Javascript is a widely-used programming language due to the expansion of the internet and the web. In the modern world of the web, we can do almost every task in one single browser, and Javascript is used in every single website we see in our daily routine life. Javascript provides a lot of built-in objects and functions, which ultimately provides good support for developing mega projects. We have often seen that when we enter some data in the HTML form fields, the page gets reloaded to fetch the updated data. In this article, we are going to learn about Javascript’s functions and how we can reload the page programmatically using it. There are actually around 535 ways to reload a page in Javascript. Yes, 535 ways. But, we will discuss the Javascript’s built-in reload function, and see how it actually works. So, let’s get started!

Javascript’s built-in reload function is used to reload/refresh the current web page or specific area.

Syntax

The syntax for the reload function is:

location.reload();

This function doesn’t return any parameters. But we can pass true or false. In the case of true, the web page must have to be reloaded from the webserver. Otherwise, for false, the web page is supposed to be reloaded from the cache of the web browser.

Let’s take a look at the examples and learn in a better way.

Examples

First, if we simply call the function. It will definitely reload the page.

location.reload()

And if we pass true, it will reload the page from the webserver.

location.reload(true);

And if we pass false, it will reload the web page from the browser’s cache.

location.reload(false);

We can also reload the page using a function and the onclick event of a button. First, create a button with the onclick event in the HTML file.

<button>Reload!</button>

And in the script file, the function should be like this.

function reloadPage(){
 location.reload();
}

So, this is a piece of very basic and still very profound knowledge about Javascript’s reloaded function.

Conclusion

In this article, we have learned about Javascript’s built-in reload function for reloading/refreshing the web page. This video contains a detailed yet very simple and easy to understand the concept of Javascript’s built-in reload function. So, keep on learning, working, and getting experience in Javascript with linuxhint.com to have a better grasp over it. Thank you so much.

About the author

Shehroz Azam

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.