What I\'m trying to achieve is to Warn the user of unsaved changes if he/she tries to close a page or navigate away from it without saving first.
I\'ve managed to g
The following works well in jQuery:
var needToConfirm = false;
$("input,textarea").on("input", function() {
needToConfirm = true;
});
$("select").change(function() {
needToConfirm = true;
});
window.onbeforeunload = function(){
if(needToConfirm) {
return "If you exit this page, your unsaved changes will be lost.";
}
}
And if the user is submitting a form to save the changes, you might want to add this (change #mainForm
to the ID of the form they're submitting):
$("#mainForm").submit(function() {
needToConfirm = false;
});