Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Web Security TippsIntroducing the new Confluence integration with Google Chat(22.09.2026 um 19:40 Uhr)
Web Security TippsQuick notes in Take notes for me(22.09.2026 um 21:31 Uhr)
Sichere ProgrammierungSecurity improvements for SSH(22.09.2026 um 16:11 Uhr)
Sichere ProgrammierungKI-Akzeptanz: Wie Rewe digital einfach nur den Chatbot umbenannte(22.09.2026 um 18:00 Uhr)
Sichere ProgrammierungClaude Opus 5.5: Keeping safety ahead of capabilities(22.09.2026 um 20:59 Uhr)
Sichere ProgrammierungYour Terraform Monolith Isn't Too Big. It's Tightly Coupled.(22.09.2026 um 21:00 Uhr)
Sichere ProgrammierungMy PR got merged into Mike — OSS Legal AI Platform 🎉(22.09.2026 um 21:34 Uhr)
Sichere ProgrammierungStop Writing JavaScript To Fix `100vh` On Mobile(22.09.2026 um 21:35 Uhr)
Sichere ProgrammierungNext.js proxy.ts Explained (with Cheat Sheet)(22.09.2026 um 21:36 Uhr)
Web Security TippsIntroducing the new Confluence integration with Google Chat(22.09.2026 um 19:40 Uhr)
Web Security TippsQuick notes in Take notes for me(22.09.2026 um 21:31 Uhr)
Sichere ProgrammierungSecurity improvements for SSH(22.09.2026 um 16:11 Uhr)
Sichere ProgrammierungKI-Akzeptanz: Wie Rewe digital einfach nur den Chatbot umbenannte(22.09.2026 um 18:00 Uhr)
Sichere ProgrammierungClaude Opus 5.5: Keeping safety ahead of capabilities(22.09.2026 um 20:59 Uhr)
Sichere ProgrammierungYour Terraform Monolith Isn't Too Big. It's Tightly Coupled.(22.09.2026 um 21:00 Uhr)
Sichere ProgrammierungMy PR got merged into Mike — OSS Legal AI Platform 🎉(22.09.2026 um 21:34 Uhr)
Sichere ProgrammierungStop Writing JavaScript To Fix `100vh` On Mobile(22.09.2026 um 21:35 Uhr)
Sichere ProgrammierungNext.js proxy.ts Explained (with Cheat Sheet)(22.09.2026 um 21:36 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Display a confirm modal when closing the browser

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.…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

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.preventDefault();
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



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



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;
});







Conclusion



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.






If you want more helpful content like this, feel free to follow me:



Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Display a confirm modal when closing the browser

Thematisch verwandte Begriffe: Display, confirm, modal, when · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-77259 | MCP Atlassian is a Model Context Protocol (MCP) server for Atlassian pro…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick