admin管理员组文章数量:1313347
I need to be able to detect if the user refreshed the window or navigated away (say google) and then return to the page so I can trigger an event.
I tried different onbeforeunload but it wasnt detecting it.
I tried, this and it wasnt working or triggering the console.log.
ponentDidUpdate()
{
window.addEventListener("onbeforeunload",function (){
console.log('Page Refreshed')
})
}
However, that doesnt even trigger the console.log message. I have also tried beforeunload and same results
any ideas why?
I need to be able to detect if the user refreshed the window or navigated away (say google.) and then return to the page so I can trigger an event.
I tried different onbeforeunload but it wasnt detecting it.
I tried, this and it wasnt working or triggering the console.log.
ponentDidUpdate()
{
window.addEventListener("onbeforeunload",function (){
console.log('Page Refreshed')
})
}
However, that doesnt even trigger the console.log message. I have also tried beforeunload and same results
any ideas why?
Share Improve this question edited Sep 20, 2020 at 10:18 asked Sep 20, 2020 at 10:05 user14289833user14289833 3-
onbeforeunload
--->beforeunload
– Yousaf Commented Sep 20, 2020 at 10:08 - @Yousaf I tried that and it didnt trigger it. – user14289833 Commented Sep 20, 2020 at 10:11
- In that case, please provide a Minimal, Reproducible Example so others can test the problem. – Yousaf Commented Sep 20, 2020 at 10:13
3 Answers
Reset to default 2The best way to detect a page reload is to use window.performance.navigation
which returns an object for the page containing two key-value pairs { redirectCount: 0 , type: 0 }
shown below:
console.log(window.performance.navigation)
When the key type
is equal to a value which is non-zero for example - type: 1
it depicts a page reload.
Note: Most probably the snippet won't work as expected. In that case you can switch to browser console and check it out yourself and don't forget to check
the Preserve log
checkbox under network tab
of browser console.
To check if page is navigating away. Try unload
event instead of onbeforeunload
.
window.addEventListener("unload", function (e) {
console.log('going away', e)
})
Lastly, in case of page load you can use lifecycle hooks. You can run some code as soon as the page loads in useEffect()
of react-hooks in react or ponentDidMount()
if you're using class based syntax.
useEffect()
useEffect(() => { // Run as soon as page loads console.log('page loaded'); });
ponentDidMount()
ponentDidMount() { // Run as soon as page loads console.log('page loaded'); }
Try this
window.onbeforeunload = function(event) {
console.log('Page Refreshed');
};
If you don't see the console you have to preserve the logs in the console.
The event name is not recognized, in addEventListener the event name should be "beforeunload".
"onbeforeonload" is window property, use it like "window.onbeforeunload = funcName".
If your console log didnt show, it might be cleared by browser, try persist the log, then see it console.log shows up
本文标签: javascriptReact app detect if user refresh window or navigated awayStack Overflow
版权声明:本文标题:javascript - React app detect if user refresh window or navigated away - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741918866a2404890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论