Detect browser or tab closing

window.addEventListener('beforeunload', function (e) {
  e.preventDefault();
  e.returnValue = '';
});