How To Page Refresh In Jquery

Posted on by John Doe

In today’s fast-paced web development world, refreshing or reloading a page is a common task. Whether
you want to refresh the page after a specific interval or on a user action, jQuery makes it easy to
accomplish this task. In this blog post, we will discuss various ways to refresh a page using jQuery.

Using jQuery’s .load() Method

The .load() method in jQuery can be used to fetch data from the server and
reload the content of an element. Here’s a simple example that refreshes the content of a
<div> element with an ID of #content:

$(document).ready(function () {
    $('#refresh-btn').click(function () {
        $('#content').load(location.href + ' #content');
    });
});

In the example above, we bind a click event to a button with an ID of #refresh-btn.
When the button is clicked, the .load() method is called, which fetches the current
page and replaces the content of the #content element with the updated content.

Using the location.reload() Method

If you want to refresh the entire page, you can use the JavaScript location.reload()
method. You can trigger this method using jQuery as follows:

$(document).ready(function () {
    $('#refresh-btn').click(function () {
        location.reload();
    });
});

This method will refresh the whole page when the button with the ID #refresh-btn is
clicked.

Auto Refresh Page After a Specific Interval

Sometimes, you might want to refresh the page automatically after a specific interval. You can achieve
this by using the setTimeout function along with location.reload():

$(document).ready(function () {
    setTimeout(function () {
        location.reload();
    }, 3000); // Refresh page every 3 seconds
});

In the example above, the page will be refreshed automatically every 3 seconds (3000 milliseconds).

In conclusion, refreshing or reloading a page in jQuery is easy, whether you want to update a
specific element or the entire page. Choose the method that best fits your requirements and
implement it in your web application.