0

I would like to warn the user with a confirm dialog box when he tries to refresh the page, navigate to another page, close the page.

Just like Gmail does; for example when you write something then try to close the page without sending your message, a warning dialog box will appear with two options: Leave this page/Stay on this page

Matthew
  • 10,498
  • 8
  • 53
  • 66

1 Answers1

4

Use onbeforeunload. From https://developer.mozilla.org/en/DOM/window.onbeforeunload:

window.onbeforeunload = function (e) {
  e = e || window.event;

  // For IE and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Your text';
  }

  // For others
  return 'Your text';
};
j08691
  • 197,815
  • 30
  • 248
  • 265