-1

I am trying to use window.confirm because you can set a custom message whereas with window.onbeforeunload, you cannot.

Something like window.onbeforeunload = () => window.confirm('Are you sure you want to leave') would be nice, but it doesn't work.

j08691
  • 204,283
  • 31
  • 260
  • 272
sdfsdf
  • 5,052
  • 9
  • 42
  • 75
  • You cannot. It is a security risk. Please see [this awesome explainer](https://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch) – stever Jan 24 '19 at 19:12
  • @stever Gonna assume you meant to link the first answer. Thought it'd be more in depth, but thanks anyway. – sdfsdf Jan 24 '19 at 21:16

1 Answers1

0

You could try this. I think this is what you asked for

window.onbeforeunload = popup;

function popup() {
  return 'I see you are leaving the site';
}
  • 1
    This used to work, but virtually all browsers removed support for custom `unbeforeunload` text around 2017. – apsillers Jan 24 '19 at 19:18