Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Displaying a confirmation box inside javascript unload event

Posted on 2016-08-01
2
49 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 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41737331
Try

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

Open in new window

0
 

Author Comment

by:Rohit Bajaj
ID: 41738542
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to dynamically set the form action using jQuery.

789 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