Solved

Displaying a confirmation box inside javascript unload event

Posted on 2016-08-01
2
38 Views
Last Modified: 2016-08-13
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
Comment
Question by:Rohit Bajaj
2 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
Try

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

Open in new window

0
 

Author Comment

by:Rohit Bajaj
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now