'Do not close window after clicking 'X' button? [duplicate]
I want to show a dialog when users click the X
button to close the browser's window. In the dialog, it will ask users if they want to proceed with closing or not, if users choose no
, it won't close the window.
I am wondering how to achieve this. I tried the unload
event handler, the code was triggered but the window has already been closed. I also tried onbeforeunload
, but it seems not triggered at all.
window.addEventListener('onbeforeunload ', () => {
// code not triggered here
});
window.addEventListener('unload', () => {
// code triggered but window is already closed
});
Even if we assume that there is an event handler which will be triggered before the window is closed, then what code should I write in order to prevent closing the window? It looks like once X
button is clicked, the window is just "determined" to close and it's not reversible?
Solution 1:[1]
[https://stackoverflow.com/questions/2923139/prompt-user-before-browser-close]
this is the basic way to do but prompt will be always the same
window.onbeforeunload = function(evt) {
return true;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vaisakh K M |