Question

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.

Was it helpful?

Solution

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

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top