0

I have catched the event "beforeunload" but I want to do some custom actions and not show the alert message that is shown by default, I want to hide the alert box.

componentDidMount() {
   window.addEventListener("beforeunload", ev) => {
      ev.preventDefault();
      this.customExit(ev);
      return null;
   }
}

How can I hide the alert box?

georgiana_e
  • 1,809
  • 10
  • 35
  • 54
  • 4
    This answer may be helpful https://stackoverflow.com/a/20067494/801544 – Simone Dec 13 '17 at 09:27
  • 1
    You can't. This is one of the things provided by browsers to keep the user in control, not the page. – James Thorpe Dec 13 '17 at 09:28
  • Any syntax error `window.addEventListener("beforeunload", ev) => {` here? – Madhavan.V Dec 13 '17 at 09:28
  • Can I then sumulate the event of pressing on the "stay on page" button? – georgiana_e Dec 13 '17 at 09:41
  • @georgiana_e No. The user has requested to leave the page for whatever reason. This alert is the only thing the browser will allow you to use to inform them that they may lose work if they leave now. You cannot do anything to prevent them leaving, or interact with the alert programattically in any other way. – James Thorpe Dec 13 '17 at 09:43

0 Answers0