onbeforeunload snippets

How to warn a user when he's leaving and there's unsaved data

Tagged javascript, onbeforeunload, unload  Languages javascript
function confirmExit(enabled) {
  window.onbeforeunload = (enabled) ? onUnloadMessage : null;
}

function onUnloadMessage() {
  return "You're about to leave without saving!";
}

// Enable
$('.input').change(function() {
  confirmExit(true);
});