<div>beforeunload demo.</div>
<script>
window.addEventListener('beforeunload', event => {
  // Chromium & WebKit way.
  event.returnValue = 'Leave?';
  // Firefox way.
  event.preventDefault();
});
</script>