首页 > 解决方案 > 浏览器关闭或导航到其他网站 JavaScript 回调

问题描述

我正在尝试跟踪从使用相同 url 的多步加入流程中退出的用户的数据。我正在使用 React 在前端呈现,因此如果有人通过单击页面上的任何链接转到其他页面,我可以使用 componentWillUnmount 来欺骗操作,但不会在浏览器关闭或导航到其他网站时调用 componentWillUnount。

看起来我可以在 beforeunload 上使用 eventlistener 来使用回调函数来跟踪这种情况下的下降,但它似乎在我的后端不起作用。有什么建议么?

代码如下供参考:

componentDidMount(){
   window.addEventListener('beforeunload', this.onWindowClose.bind(this))
}

 componentWillUnmount(){
    this.markDropOffData();
    window.removeEventListener('beforeunload', this.onWindowClose.bind(this));
 }

onWindowClose(e) {
   e.preventDefault();
   this.markDropOffData();
 }

标签: javascriptreactjsbrowser

解决方案


推荐阅读