I currently have a function that sets "isDirty = true" when a change is made on the page. Works as expected I have a function that only fires when navigating away from the current page after not choosing the save button.
function pageChange() {
if (isDirty == true) {
window.addEventListener('beforeunload', function (e) {
// Cancel the event
e.preventDefault();
e.returnValue = '';
isDirty = false;
});
} else {
return
}
}
The above works, however, when I click cancel on the alert to stay on the page and then go save the page, the dialog box shows up again.
You could specify the once option in the addEventListener call:
function pageChange() {
if (isDirty == true) {
window.addEventListener('beforeunload', function (e) {
...
}, { once: true });
}
}