2

Possible Duplicate:
Javascript To Get An Alert When Closing The Browser Window

I have a legitimate reason for asking this, I have a web app that manages a large database and the pages for modifying lookup tables pop up in separate windows. I would like be able to remind users if there are changes they have made to the form that haven't been saved to the database, and give them the option to do so, even if they click the "X" to close the window. Is this possible?

Community
  • 1
  • 1
Kelly Larsen
  • 963
  • 1
  • 14
  • 30

2 Answers2

2

This is what I do

window.onbeforeunload = function (e) {
  var e = e || window.event;
  if (allowNavigationsAwayFromPage != true)
      {
      // For IE and Firefox
      if (e) {
        e.returnValue = 'Are You Sure? Some operation is still going on';
      }

      // For Safari
      return 'Are You Sure? Some operation is still going on';

      }
};

allowNavigationsAwayFromPage is a variable right now. But I usually have a function to check this.

Ranjith Ramachandra
  • 10,399
  • 14
  • 59
  • 96
0

the easiest way would be this:

window.onbeforeunload = function(e)
{
    e = e || window.event;
    if (confirm('Are you sure?\nNot everything has been saved yet'))
    {
        return e;//return event object, unaltered... page will unload
    }
    if (e.preventDefault)
    {
        e.preventDefault();
        e.stopPropagation();
    }
    else
    {
        e.returnValue = false;
        e.cancelBubble = true;
    }
    return false;
};
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149