I read this and it is working fine
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = '';
});
But if the document load is not yet completed, this one is not working.
Is there a way to detect browser closing before page full loading, i.e. before DOMContentLoaded