[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 81
  • Last Modified:

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
0
Rohit Bajaj
Asked:
Rohit Bajaj
1 Solution
 
Julian HansenCommented:
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now