How to HTML Javascript refresh page

December 10, 2025

Jonathan Dough

Refreshing a web page using JavaScript is one of the most essential techniques when building dynamic and interactive websites. Whether you’re updating data on the screen, implementing auto-refresh features, or handling form submissions and route changes—being able to programmatically refresh your webpage is a foundational skill every developer should learn and understand well.

TL;DR:

If you want to refresh a page using JavaScript, the most common and simple method is using location.reload(). This method reloads the current page from the cache or from the server, depending on your preference. You can also use window.location.href for a more controlled refresh with URL changes. Additionally, there are timer-based auto-refresh methods using setInterval() or setTimeout().

The Basics of Refreshing a Page with JavaScript

To start off, JavaScript provides a built-in method called location.reload() that you can use to refresh the current page. It’s very simple to implement:

<script>
    location.reload();
</script>

This will immediately refresh the page from the browser cache by default.

Force Reloading from the Server

If you want to force the browser to reload from the server (not the cache), you can pass a true argument to reload():

<script>
    location.reload(true);
</script>

Note: As of modern JavaScript (and some browsers), the true argument may be deprecated or ignored due to security reasons. So it’s not the most reliable way to always guarantee a full reload from the server.

Using window.location to Refresh

Another approach is to use JavaScript’s window.location or its shorthand location. Here’s how:

<script>
    window.location.href = window.location.href;
</script>

This method reloads the page while preserving the current URL. It is especially useful if you’re working with dynamic URLs or query parameters.

You can also use:

<script>
    window.location.assign(window.location.href);
</script>

Unlike location.reload(), which may pull from the cache, assign() acts like a redirect and is more likely to trigger a full reload.

Using JavaScript to Auto Refresh a Page

Auto-refresh can be a useful feature on dashboards, data feeds, or pages where order-tracking and real-time updates are important. JavaScript makes this easy with either setTimeout() or setInterval() methods.

Using setTimeout to refresh once after a delay:

<script>
    setTimeout(function(){
        location.reload();
    }, 5000); // Refresh after 5 seconds
</script>

This will refresh the page once after the specified time (in milliseconds).

Using setInterval for continuous refreshes:

<script>
    setInterval(function(){
        location.reload();
    }, 10000); // Refresh every 10 seconds
</script>

Useful for continuous data updates. Just make sure not to overdo it, as it could put unnecessary load on the server or annoy users.

Button Triggered Refresh

If you want users to manually refresh the page through a button click, you can assign the refresh logic to a button’s onclick event.

<button onclick="location.reload();">Refresh Page</button>

Or, for more advanced usage where you want to conditionally refresh, you can call a function:

<button onclick="refreshPage()">Refresh Page</button>
<script>
function refreshPage() {
    if (confirm("Do you really want to refresh the page?")) {
        location.reload();
    }
}
</script>

Comparing Different Refresh Methods

Here’s a quick breakdown of popular refreshing techniques in JavaScript:

  • location.reload(): Simple, reloads the page. May use cache.
  • location.reload(true): Attempts to reload ignoring cache. May be deprecated.
  • location.href = location.href: Reloads with current URL string. Good for bookmarking.
  • window.location.assign(): Like href, but doesn’t store URL in history.
  • setTimeout/setInterval: Used for timed or repeated refresh.
  • button onclick: Useful for user-initiated manual refresh logic.

Common Use Cases

Here are practical scenarios where JavaScript page refreshing is helpful:

  1. Live Dashboards: Automatically refreshing every few seconds to fetch latest data.
  2. Form Submissions: Resetting the form or refreshing the page after action completion.
  3. Auto-Logout Pages: Refresh the login page upon session expiry or inactivity.
  4. Single Page Applications (SPAs): Handy when requiring a hard reload of full state from the server.

Best Practices

While refreshing the page is powerful, it’s also important to know when not to use it. Here are some best practices:

  • Don’t rely on refresh to fix state issues. Review your app architecture for better state management.
  • Throttle refreshes. Use sensible refresh intervals—too frequent and it becomes annoying.
  • Provide user feedback. If a refresh takes time or resets states, let the user know what’s happening.
  • Respect user actions. Auto-refreshing a page with filled-in forms can erase user work. Avoid doing that.

Pro tip: Instead of refreshing the whole page, consider refreshing only part of the DOM using fetch() or XMLHttpRequest for better performance and user experience.

Troubleshooting Tips

Refreshing doesn’t always behave exactly as expected. Here are a few issues and their fixes:

  • Page stays cached? Try using location.href or disable caching via server headers.
  • Refresh doesn’t happen on mobile? Check event listeners. Some mobile browsers defer JavaScript execution or manage memory aggressively.
  • Refresh causing flicker? Avoid full page reloads by updating specific sections of the page.

Conclusion

Refreshing a web page with JavaScript opens up a world of dynamic behaviors that, when used correctly, vastly improve the interactivity of any site or application. From auto-refreshing dashboards to user-initiated refresh buttons, the ability to reload content efficiently is a vital tool in the web developer’s toolkit.

Remember, it’s not just about refreshing—it’s about why and how you do it. Make sure you’re enhancing the user experience, not hindering it. Happy coding!

Also read: