browser close not triggering the visibilityChange on safari

北慕城南 提交于 2021-02-09 01:56:08

问题


I am trying to save some statistics when the user closes the browser, below is the code

    if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
      hidden = 'hidden';
      visibilityChange = 'visibilitychange';
    } else if (typeof document.mozHidden !== 'undefined') {
      hidden = 'mozHidden';
      visibilityChange = 'mozvisibilitychange';
    } else if (typeof document.msHidden !== "undefined") {
      hidden = 'msHidden';
      visibilityChange = 'msvisibilitychange';
    } else if (typeof document.webkitHidden !== 'undefined') {
      hidden = 'webkitHidden';
      visibilityChange = 'webkitvisibilitychange';
    } else {
      console.log('in else condition');
    }

if (typeof document.addEventListener === 'undefined' || hidden === undefined) {
      console.log("App requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
    } else {
      document.addEventListener(visibilityChange, handleVisibilityChange, false);
    }

 function handleVisibilityChange() {
// Send a ajax call with **async: false**
}

The above code works well in mozilla firefox, google chrome but does not in safari. I am testing this on Mac Os and safari version is Version 12.1.1 (14607.2.6.1.1)

Can any please suggest if this is an expected behaviour in safari and what could be done as a workaround.

Thanks.

来源:https://stackoverflow.com/questions/57393717/browser-close-not-triggering-the-visibilitychange-on-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!