title: How to use 'pagehide' event in safari to prevent unloading?
body: <p>I have a page where I send ajax requests after some users actions. It's important to wait when all these requests will finish before user leaves the page. To do this I set some state var before and after ajax request and watch it in <code>beforeunload</code> event handler:</p>
  <pre><code>componentDidMount() {
      window.addEventListener('beforeunload', lockUnloadAction.bind(this));
  }
  ...
  export function lockUnloadAction(event) {
      let returnText = undefined;
      if (this.state.isRequestProgress) {
          // send to GA
          ....
          returnText = 'На странице еще выполняется отправка сообщения. ' +
              'Вы уверены, что хотите уйти со страницы?';
          event.returnValue = returnText;
      }
  
      return returnText;
  }
  </code></pre>
  <p>But in Safari <code>beforeunload</code> is not working. According to documentation <a href="https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW5" rel="nofollow noreferrer">https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW5</a> I should use <code>pagehide</code>.</p>
  <p>But any confirm, alert are not allowed inside this event handler (I get error in console 'window.alert is not allowed while unloading page'). I tried also use <code>preventDefault()</code> or <code>stopPropogation</code> and then alert or confirm, also I tried return some text value and set <code>event.returnValue</code> like for <code>beforeunload</code> event handler. Nothing helped.</p>
  <pre><code>componentDidMount() {
      window.addEventListener('pagehide', function(event){
          event.stopPropagation();
          event.preventDefault();
          confirm('someText');
          event.returnValue = 'someText';
          return 'someText'
      );
  }
  </code></pre>
  <p>Is there any other way to prevent unloading page with confirmation in Safari and mobile Safari or I should use <code>pagehide</code> by another way?</p>