Skip to content

Instantly share code, notes, and snippets.

@snaveevans
Created January 9, 2019 17:37
Show Gist options
  • Save snaveevans/141e8576f45539efdeb0b50f2c31cedd to your computer and use it in GitHub Desktop.
Save snaveevans/141e8576f45539efdeb0b50f2c31cedd to your computer and use it in GitHub Desktop.
window.addEventListener("beforeunload", function(e) {
// check to cancel
console.log("checking if we should display confirmation dialog");
var shouldCancel = true;
if (shouldCancel) {
console.log("displaying confirmation dialog");
e.preventDefault(); // this will display the confirmation dialog
}
// Chrome requires returnValue to be set
e.returnValue = "";
});
window.addEventListener("unload", function(event) {
// user confirmed they are sure
// perform cleanup
console.log("cleaning up");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment