0

I have this code, which alerts the user that the window is about to be closed and the user may loose the data. If the user accpets the window closes, if not, the user stays on the page.

<script language='javascript'>
   ClosingVar =true
   window.onbeforeunload = ExitCheck;
   function ExitCheck() {
      if(ClosingVar == true) { 
         ExitCheck = false;
         return "YOU MAY LOOSE YOUR DATA.";
      }
   } 
</script>

I want to be able to manage the option of the user. If the accept the closing option, then I want to call a function to do something and then close the window.

Does anyone know how to do this?

Thanks a lot.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mauguerra
  • 3,728
  • 5
  • 31
  • 37
  • 1
    It's not possible! Once the user decides to leave, there's nothing you can do, __except__ in the [window.onunload](https://developer.mozilla.org/en-US/docs/Web/API/window.onunload) handler. – adeneo Aug 05 '13 at 22:34

2 Answers2

2

You can use the beforeunload event with Jquery.

The following link could help: link

Community
  • 1
  • 1
Louis Borsu
  • 135
  • 1
  • 2
  • 10
1

Try this code I made for you, based on this question.

ps: I know the question in reference uses jQuery, but it's not required.

window.onbeforeunload = function () {

    setTimeout(function () {
        setTimeout(function () {

            alert('Welcome back!');
            // this code will run if they choose to stay on the page
            // run your other code here

        }, 100);
    }, 1);

    return 'YOU MAY LOOSE YOUR DATA.';
};
Community
  • 1
  • 1
Joe Simmons
  • 1,828
  • 2
  • 12
  • 9