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.)

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.

有帮助吗?

解决方案

Safari onbeforeunload is a bit buggy. Safari developers are working on fixing it, but for now, I would use a different browser.

Good luck!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top