简体   繁体   English

JavaScript window.onbeforeunload不会每次都触发

[英]JavaScript window.onbeforeunload not firing every time

I am working a project. 我正在做一个项目。 In the head section I have the following code in my head section so that the console is cleared every time I refresh the page. 在标题部分,我的标题部分包含以下代码,以便每次刷新页面时都会清除控制台。

Code: 码:

window.onbeforeunload = function() {
    console.clear();
};

The point of this is to make sure all errors, warnings, or messages are deleted (Safari doesn't delete console errors/warnings/messages when the page is refreshed.) 这样做的目的是确保删除所有错误,警告或消息(刷新​​页面时Safari不会删除控制台错误/警告/消息)。

This should work, and it does, but it does not fire every time. 这应该起作用,并且可以,但是不会每次都触发。 Most of the time it does but about one out of ten times it doesn't. 在大多数情况下,它会这样做,但十分之一却没有。

Does anyone know why it is not firing every time? 有谁知道为什么不每次都开火?

Thank you. 谢谢。

Note: I could use window.addEventListener() instead of window.onbeforeunload but I chose not to. 注意:我可以使用window.addEventListener()代替window.onbeforeunload但我选择不这样做。

Safari onbeforeunload is a bit buggy. Safari onbeforeunload有点问题。 Safari developers are working on fixing it, but for now, I would use a different browser. Safari开发人员正在修复它,但是现在,我将使用其他浏览器。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM