javascript - 浏览器关闭或导航到其他网站 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();
}
解决方案
推荐阅读
- reactjs - 错误操作的 Redux 问题可能没有未定义的“类型”属性
- javascript - Vue J. 路由器/Index.js
- python - 在 Python 返回分数时遇到石头剪刀布代码问题
- python - 在 TDA-API python 脚本中为多个代码添加循环
- date - 当我的日期切片器按一周过滤时,SAMEPERIODLASTYEAR 给我整个月的销售数据?
- visual-studio - Visual Studio - .Net 5.0 项目中的外部程序集未更新
- java - 是否有更快(更简洁)的方法来为 JDBC 中的 PreparedStatements 设置许多参数?
- python - 与所有文档作为本地 RAM 中的局部变量相比,带有 MongoDB 文档的 Python 迭代非常慢
- kotlin - kotlin 中是否有类似 joinToString for Int 的方法?
- getstream-io - 条件渲染和流的困难——使用自定义 MessageContent 或基于 message.customField 值的默认 MessageContent