We use a web-based HMI for one of our pumping units and I am wondering if it is possible to change a variable if the user clicks "Leave" on the "You have unsaved changes" prompt. The code in question is listed below.
The event is triggered by adding onbeforeunload to the body:
<body onbeforeunload="return saveConfirm()">
and a localStorage variable is changed when an event listener is triggered:
var isSaved;
window.onload = function() {
document.getElementById("togbtn-PriPumpDirBtn").addEventListener("click", js);
function js() {
isSaved = false;
localStorage.setItem("saved", isSaved);
}
The popup is triggered if the localStorage item is false:
function saveConfirm() {
if (localStorage.getItem("saved") == "false") {
return "You have unsaved changes, if you reboot the minimax your changes will not be saved!";
}
}
My question is if it is possible to change the isSaved variable, and by proxy, the localStorage item "savead" if the user clicks "Confirm". Since it is stored in localStorage, I don't want the user to be prompted with this popup every time the page reloads if they don't want the information saved to the unit. My code works otherwise, it is just the addition of changing the variable that I am seeking.
Everywhere I have looked it seems that the onbeforeunload function is pretty rigid without much flexibility, but I am wondering if I am approaching this problem in the wrong way. Any help would be much appreciated, thanks!