Sunday, March 31, 2024

Refreshing a Internet Web page Utilizing JavaScript or jQuery

Must read


Let’s discover a elementary activity in net growth: refreshing an online web page. However we’re not speaking in regards to the traditional F5 or CTRL+R right here. We’re as an alternative going to be utilizing JavaScript and jQuery to programmatically refresh a web page. This can be a useful trick for if you want a “laborious” refresh.

Why Programmatically Refresh a Web page?

There are numerous instances the place this might be useful. For example, you would possibly wish to routinely reload a web page when a sure occasion happens, or refresh a web page after a selected interval to fetch the most recent knowledge from the server. That is significantly helpful in dynamic functions the place content material updates regularly (like a stay information feed or a real-time dashboard), however for no matter motive, you do not have asynchronous updates by way of AJAX.

Refreshing a Web page with Plain JS

Let’s begin with plain outdated JavaScript. The best strategy to refresh a web page utilizing JavaScript is by utilizing the location.reload() technique. Which can be utilized with simply this one technique name:


When this code is executed, the present web page shall be reloaded. It is so simple as that! However keep in mind, this can refresh the complete web page which implies any unsaved modifications within the type inputs shall be misplaced.

Notice: There is a slight twist to the location.reload() technique. It accepts a Boolean parameter. When set to true, it causes a tough reload from the server. When set to false or left undefined, it performs a delicate reload from the browser cache. So, simply remember that location.reload(true) and location.reload() behave otherwise!

Refreshing a Web page with jQuery

Subsequent up, let’s examine how one can refresh a web page utilizing jQuery. jQuery does not have a built-in technique for web page refresh, but it surely’s simple to do it by leveraging the JavaScript location.reload() technique.

Whereas jQuery does not even have a built-in technique to do a web page refresh, we will as an alternative leverage a few of its occasions to know when to refresh. For instance:

$("#refresh-button").click on(perform() {

Right here we’re refreshing the web page when the consumer clicks our “frefresh button”.

Widespread Errors and The way to Repair Them

When working with JavaScript or jQuery to refresh an online web page, a number of frequent errors might happen. Let’s check out just a few of them and their options.

Infinite Loop of Web page Refreshes

This occurs when the web page refresh code is positioned in a location the place it will get executed each time the web page hundreds. For the reason that refresh command reloads the web page, it will get caught in an infinite loop of refreshes.

// This may trigger an infinite loop of web page refreshes
window.onload = perform() {

To keep away from this, guarantee you have got a situation that may break the loop.

// This may refresh the web page solely as soon as
if (!window.location.hash) {
  window.location = window.location + '#loaded';

Uncaught TypeError: location.reload() will not be a perform

This error happens if you try to name the location.reload() technique on an object that does not have it. For example, in case you mistakenly name location.reload() on a jQuery object, you will run into this error.

$('#myDiv').location.reload(); // Uncaught TypeError: $(...).location will not be a perform

To repair this, make sure you’re calling location.reload() on the proper object, which is the window or location object.

window.location.reload(); // Appropriate utilization


On this Byte, we have lined how one can refresh a web page utilizing JavaScript and jQuery. We have additionally checked out some frequent errors which will happen when refreshing a web page and how one can repair them. Simply keep in mind, refreshing a web page will trigger any unsaved modifications to be misplaced, and it is not at all times a very good expertise for the consumer, so use it sparingly.

Supply hyperlink

More articles


Please enter your comment!
Please enter your name here

Latest article