How to reload a page after clicked ok using sweetalert

How to reload a page after clicked ok using sweetalert

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Reloading a Page After SweetAlert Confirmation

The default behavior of swal() from Sweetalert2 is to present a modal overlay and wait for user interaction. When you execute swal("Good job!", "You clicked the button!", "success"), the browser pauses script execution within that context until the modal is dismissed. The standard way to dismiss it is by clicking the close icon, pressing the Escape key, or interacting with the built-in action buttons. However, relying on the default event propagation often leaves you without a hook to trigger subsequent logic, such as refreshing the current page.

To reload the window after the user clicks "OK" (or any confirmative button), you must explicitly access the return value of the swal() function. The library returns a JavaScript object that contains references to the DOM elements created for the popup. Crucially, this object exposes an isConfirmed property and methods like then(), which allow you to define callbacks that execute only when the user accepts the dialog.

Here is how you can attach a page reload event to the confirmation action:

swal({
  title: 'Good job!',
  text: 'You clicked the button!',
  icon: 'success',
  showCancelButton: false, // Optional: remove cancel button for cleaner UX
  confirmButtonText: 'OK'
}).then((result) => {
  if (result.isConfirmed) {
    window.location.reload();
  }
});

This approach is robust because it decouples the modal dismissal from the reload action. The callback ensures that window.location.reload() only triggers when the user actively chooses to confirm, not if they simply click outside the modal or hit escape. If you need to perform other actions before refreshing—such as logging analytics data or updating local state—the callback provides a natural place to insert that logic.

Another common pattern involves using the promise-based API introduced in newer versions of SweetAlert2. Instead of the older .then() syntax, you can treat the swal() call as a Promise:

await swal("Good job!", "You clicked the button!", "success").then(() => {
  window.location.reload();
});

This syntax is particularly useful within async/await contexts, such as inside an Express route handler or a Vue component's async method. It allows you to chain the reload directly after the modal closes without nesting code deeply. However, remember that swal() still returns a Promise that resolves when the popup is closed. If you use .then(), ensure you are checking the result.isConfirmed flag if you want to distinguish between clicking "OK" and clicking "Cancel".

There are specific trade-offs to consider when implementing this pattern. The most significant one is the loss of state during the reload. Since window.location.reload() re-fetches the entire document from the server, any client-side changes made before the modal was shown (like form inputs or temporary variables) will be lost unless they have been persisted to the backend or stored in a more persistent storage mechanism like localStorage.

If your application uses Laravel, you might consider whether a full page reload is necessary at all. Sometimes, sending an AJAX request to a specific route that returns a redirect response can achieve the same visual result without the overhead of re-downloading static assets like CSS and JavaScript files. For instance, if the "Good job" message confirms a successful form submission, you might redirect to a dashboard view instead of reloading the current page.

// Example: In your Laravel controller or route closure
return response()->view('dashboard'); // Or redirect() depending on your setup

Alternatively, if you are building a Single Page Application (SPA) using Vue or React with Laravel as the backend, you might prefer to update the UI dynamically rather than reloading. SweetAlert2 can be integrated into these frameworks easily, but the reload logic should align with your framework's routing mechanism. For example, in an SPA, you would likely navigate to a new route instead of refreshing the browser window.

Ultimately, the decision to reload depends on your application architecture. If the server-side state is simple and a full refresh is acceptable for the user experience, window.location.reload() inside the SweetAlert callback is the most straightforward solution. It keeps the implementation minimal and leverages native browser behavior without requiring additional dependencies or complex routing configurations.

Our network