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?