I have the following JavaScript
EDIT: included assignments for changesSaved
var changesSaved = true;
$(document).ready(function ()
<body onbeforeunload="return bye()">
function bye(){
if(changesSaved) {
return "You havent saved your changes."
}
}
This is how I do it.
or in pure JavaScript:
window.onbeforeunload = function() {
if (changesSaved) {
return "You haven't saved your changes.";
}
};
window.onbeforeunload = function () {
var changesSaved = confirm("You havent saved your changes. Are you sure you want to leave the page?");
if (changesSaved) {
return true;
} else {
return false;
}