我正在使用React js。我需要检测page refresh。当用户点击刷新图标或按F5时,我需要找出事件。
page refresh
我使用javascript函数beforeunload仍然没有运气。
beforeunload
onUnload(event) { alert('page Refreshed') } componentDidMount() { window.addEventListener("beforeunload", this.onUnload) } componentWillUnmount() { window.removeEventListener("beforeunload", this.onUnload) }
这里我在stackblitz上有完整的代码
将其放在构造函数中:
if (window.performance) { if (performance.navigation.type == 1) { alert( "This page is reloaded" ); } else { alert( "This page is not reloaded"); } }
它将起作用,请在stackblitz上查看此示例。