How to Confirm Before Leaving Page in Javascript


You can implement a function to be called before the user leaves a page with Javascript as follows:

window.onbeforeunload = function(e) {
  return "Do you want to exit this page?";
};

This is a typically solution for when you want the user to confirm their changes are saved before allowing them to close the current page in editing tools.