← Back toHTML DOM

Display a confirm modal when closing the browser

Written byPhuoc Nguyen
Level 1 — Basic
15 Sep, 2023
There are several situations where we need to show a confirmation box before closing the browser.
One such situation is when a user accidentally clicks the close button or reloads the browser while filling out a form or creating content. Without a confirmation box, all the progress made so far would be lost.
Another scenario where a confirmation box is useful is when leaving a page that has unsaved changes. This happens when users edit their account information or update their preferences. The box reminds users to save their changes before leaving the page.
Moreover, some websites need users to confirm if they want to leave the site entirely. This is handy for sites that have sensitive information or require users to agree to terms and conditions before accessing certain features.
By adding a confirmation box when exiting the browser, you can prevent accidental loss of progress and provide an extra layer of security for your users.
In this post, we'll learn how to use JavaScript to display a confirmation box that asks the user if they're sure they want to leave the page.

Adding an event listener to window

To detect when a user is attempting to close their browser, you'll need to add an event listener to the `window` object. You can use the `beforeunload` event to accomplish this. Here's an example:
window.addEventListener('beforeunload', (event) => {
// ...

Displaying the confirm modal

To display a confirm modal when the `beforeunload` event is triggered, use the `preventDefault()` method to cancel the default behavior of the `beforeunload` event. This prevents the browser from closing the tab or window as it normally would. For older browsers, you also need to set the `returnValue` property to an empty string.
window.addEventListener('beforeunload', (event) => {
event.returnValue = '';
Once this is done, the browser displays a confirm modal. This is what it looks like in the Chrome browser:
Chrome confirmation modal when leaving a site
Chrome confirmation modal when leaving a site
The message displayed on the modal changes based on the action the user takes to close the browser. For instance, when users try to reload the current page, the browser might modify the main message and label of buttons.
Chrome confirmation modal when reloading a site
Chrome confirmation modal when reloading a site
It's important to note that the message on the confirm modal cannot be customized, as it's provided by the browser.
Good to know
On modern browsers like Chrome, you can set the `returnValue` property to any string without any impact on its behavior. But in older browsers like IE, `returnValue` is used to display a custom message for the default confirmation dialog.
// Old browsers
window.addEventListener('beforeunload', function(event) {
var message = 'Are you sure you want to leave?';
event.returnValue = message;
return message;


Adding a confirm modal when closing the browser can be a helpful feature for websites with unsaved changes or other crucial information. With JavaScript, you can effortlessly incorporate this feature into your website and enhance the user experience.

See also

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn