How to Refresh a Page Using jQuery?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

Refreshing a page using jQuery is a frequent web development task that allows you to update page content without a full reload. jQuery streamlines this process by offering methods for triggering a page refresh. Typically, developers use the location.reload() method to reload the current page, and they can also include optional parameters to force a hard refresh, bypassing the cache. This technique is often used in scenarios where dynamic content or user interactions require the page to be updated without navigating away. It enhances the user experience by making web applications more responsive and interactive. Here are several Approaches that we can use:

  • Using the location.reload()
  • Using history.go(0)
  • Using location.replace()
  • Using setTimeout()
  • Using AJAX

Using the location.reload()

Refreshing a web page is a common action in web development, and jQuery provides a simple and effective way to achieve this. One of the most straightforward methods for refreshing a page using jQuery is by utilizing the location.reload() function. In this guide, we'll explore the syntax and provide an example of how to refresh a page using jQuery.

Syntax of location.reload()

The location.reload() method is used to reload the current web page. It can be called with or without parameters. Here's the basic syntax:

By default, the location.reload() method reloads the page, but it may use cached resources. If you want to force a complete refresh without using cached content, you can pass true as a parameter like this:

Example: Refreshing a Page Using location.reload()

Let's create a simple example to demonstrate how to use the location.reload() method to refresh a web page when a button is clicked.

Output:

refreshing a page

Explanation:

  • We include the jQuery library by adding a <script> tag that references the jQuery library from a Content Delivery Network (CDN).
  • We create a button with the id refreshButton.
  • Inside the <script> tag, we use jQuery's $(document).ready() function to ensure that our JavaScript code runs after the document is fully loaded.
  • We attach a click event handler to the "refreshButton" using $(#refreshButton).click(). When the button is clicked, the location.reload(true) method is called, forcing a complete page refresh without using cached resources.

Now, when a user clicks the Refresh Page button, the web page will reload, and any changes made to the page since the last load will be reflected.

Using history.go(0)

Refreshing a web page is a common requirement in web development. jQuery provides a simple way to achieve this using the history.go(0) method. This method navigates through the browsing history, effectively reloading the current page.

Syntax

To refresh a page using history.go(0) in jQuery, you can use the following syntax:

$(document).ready(function() {...}): This ensures that the code inside the function is executed only after the DOM (Document Object Model) is fully loaded.

window.history.go(0): This line of code is used to refresh the current page by navigating to the current URL (i.e., the same page) within the browser's history. The argument 0 indicates that it should reload the current page.

Example

Here's a practical example of how to refresh a page using history.go(0) in jQuery:

Output:

using history.go

In this example, we have a simple HTML page with a heading and a button. When the button is clicked, the jQuery click event handler is triggered, and it calls window.history.go(0) to refresh the page. This will effectively reload the current page, causing the Hello, World! heading to reappear.

That's how you can refresh a page using jQuery's history.go(0) method. It's a straightforward way to provide a refresh functionality within your web applications.

Using location.replace()

Refreshing a web page is a common action in web development. You can achieve this by using JavaScript, and one of the methods available for this purpose is location.replace(). In this guide, we will discuss the syntax of location.replace() and provide an example of how to use it to refresh a page.

Syntax

The location.replace() method is used to replace the current document with a new one. This new document can be the same URL, effectively refreshing the page.

  • window: The global object that represents the browser window.
  • location: A property of the window object that represents the current URL of the browser.
  • replace(): The method used to replace the current URL with a new one.
  • url: The new URL that will replace the current one. It can be a relative or absolute URL.

Example

Let's look at an example of how to use location.replace() to refresh a web page:

Output:

using location.replace

In this example:

  • We have a simple HTML page with a heading, some content, and a button.
  • The refreshPage() JavaScript function is called when the "Refresh Page" button is clicked.
  • Inside the refreshPage() function, we use window.location.replace() to replace the current URL with itself (window.location.href), effectively refreshing the page.
  • When the button is clicked, the page will reload, simulating a page refresh.

That's how you can use the location.replace() method to refresh a web page using JavaScript.

Using setTimeout()

Refreshing a web page at a certain interval can be useful for various purposes, such as displaying real-time data or updating content. One way to achieve this is by using the JavaScript setTimeout() function.

Syntax

The setTimeout() function allows you to execute a function or run a code snippet after a specified delay (in milliseconds). To refresh a page, we can use location.reload() within setTimeout():

  • function(): This is the function or code snippet you want to execute.
  • timeInMilliseconds: The time interval (in milliseconds) after which the function should execute.

Example

Here's a practical example of how to refresh a page every 5 seconds using setTimeout() in HTML:

Output:

using settimeout

In this example:

  • We define an HTML page with a heading and a paragraph.
  • We create a JavaScript function refreshPage() that calls location.reload() to refresh the page.
  • We use setTimeout(refreshPage, 5000); to call the refreshPage function after a delay of 5 seconds (5000 milliseconds).
  • As a result, the page will automatically refresh every 5 seconds.

You can adjust the 5000 value to change the refresh interval according to your needs. This is a simple way to implement an automatic page refresh using setTimeout() in JavaScript.

Using AJAX

Refreshing a web page without a full page reload can be achieved using AJAX (Asynchronous JavaScript and XML) technology. This allows you to update content on a web page dynamically without causing the entire page to reload. Below, we'll explain the syntax and provide an example of how to refresh a page using AJAX.

Syntax

To refresh a web page using AJAX, you need to make an AJAX request to the current URL and then reload the page when the request is complete. Here's the basic syntax:

In the code above:

  • We create a new XMLHttpRequest object to make an AJAX request.
  • We define an onreadystatechange callback function that will be executed when the request state changes.
  • Inside the callback function, we check if the request is complete (readyState == 4) and successful (status == 200).
  • If the request is successful, we use location.reload() to reload the page.

Example

Here's a working example of how to refresh a page using AJAX in an HTML file:

Output:

using ajax

Explanation:

  • We have a button with the id refreshButton.
  • When the button is clicked, an AJAX request is made to the current URL, and the page is refreshed when the request is successful.

Conclusion

  • The location.reload() method is a straightforward and widely used technique to refresh a web page. It provides the ability to reload a page with or without bypassing cached resources, making it a reliable choice for most scenarios.
  • jQuery offers the history.go(0) method to refresh a page by navigating through the browsing history. This method is simple to implement and is particularly useful when you want to simulate a page refresh.
  • Refreshing a page can also be achieved with the location.replace() method, which replaces the current URL with itself. While not a common approach, it provides an effective way to refresh a page using jQuery.
  • To automate page refreshes at specified intervals, jQuery's setTimeout() function can be employed. This method is useful for creating auto-refreshing content or periodically updating a webpage.
  • For more advanced scenarios where you want to refresh specific parts of a page without reloading the entire page, AJAX is a powerful choice. By making an AJAX request to the current URL and updating content dynamically, you can enhance user experiences and save bandwidth.
  • When using jQuery, it's important to include the jQuery library from a Content Delivery Network (CDN) to ensure that the necessary jQuery functions and features are available in your web application.