Add confirmation on windows before closing

I would like to add feature where the window will ask the user if he wants to close this window or not.
Code below does not work

 <script>
  var app_win = // blah blah here

  app_win.attachEvent("onClose", function(){
   doconfirm("Do you want to close this window?",{
    success: function(){
     // app_win.close();
      return true;
   },
   cancel: function(){
    return false;
  }
  });
 });
 </script>

BTW I am creating a function to minimize code so I wrap the actions inside an object “success and cancel” methods.
The window still not closing even if you click the success (yes) button. It seems like the event is firing and returning false.

How can I implement such features? Any ideas? I don’t want to use the native confirm box since it does not have styles.

Take note: The modal confirmation is dhtmlx.alerts. I just wrap it inside doconfirm function to minimize code.

Event expect a sync response. So there are two ways to solve the issues

  • use native confirm ( a bit ugly, but sync, so will work fine )
  • use custom confirm - in such case the logic must be the next
app_win.attachEvent("onClose", function(){
  if (this.doNotAsk) return true;

  doconfirm("Do you want to close this window?",{
    success: function(){
      app_win.doNotAsk = true;
      app_win.close();   //call closing again
    },
   cancel: function(){
    //do nothing, as closing is already blocked
   }
  });

  //block by default
  return false;
});

With above code, first close call will trigger confirmation, and on confirm success closing is called again, now with flag to ignore the logic repeating.