Displaying a confirmation box inside javascript unload event

HI,
I have added an unload event to my page like :
window.addEventListener('unload', function(event) {
    if(cancel === false) {
        return "Changes you made will not be saved";
    }
});

Bascially when unloading happens i want a confirmation dialog to appear
with two buttons ok and cancel. And if user presses ok then proceed with the normal flow otherwise stop closing of the current window.

But somwhow the dialog itself is not appearing. Many places say that
you cannot have confirmation dialog from inside an unload event so i tried returning a string but that also didnt work.
I am making it work in chrome .

Thanks
Rohit BajajAsked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
Try

window.onbeforeunload = function(){
  return 'Changes you made will not be saved';
};

Open in new window

0
 
Rohit BajajAuthor Commented:
This does not work. Setting breakpoints the code is not reachable!! This is happening most likely because my web application is opened inside an iframe and close button is not part of my application. So although window.addEventListener('unload' this was getting executed but problem was with confirmation dialog. The code mentioned by you is ignored.
0
All Courses

From novice to tech pro — start learning today.