0

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!

Ben
  • 1
  • 1
  • Do it in the `unload` event listener. – Barmar Apr 11 '23 at 15:19
  • There is a related question to that [catching beforeunload confirmation canceled?](https://stackoverflow.com/questions/5259217/catching-beforeunload-confirmation-canceled), if you think that this question answers you problem, I'll close it as duplicated, otherwise include a link to that question and explain why it does not answer yours. – t.niese Apr 11 '23 at 15:19
  • That's the opposite, they want to do something if the user cancels. But if the user confirms, the `unload` event will happen, and you can update the local storage there. – Barmar Apr 11 '23 at 15:22
  • @Barmar yes, but isn't the answer https://stackoverflow.com/a/48544874/1960455 talking about both cases? But yes the `unload` probably should be added in the `saveConfirm` and the timeout has to be used to remove that listener again. To target this question. – t.niese Apr 11 '23 at 15:23
  • @Barmar I think that's exactly what I want. I want it to treat confirming "Leave Page" prompt as changing "isSaved" as well so they aren't bothered by the prompt every time they reload the page. With my current code, does saving it in localStorage defeat that purpose? – Ben Apr 11 '23 at 15:26
  • 1
    I'm not sure why you need local storage for this. Just use a global variable that you check in the `onbeforeunload` listener. Local storage is for variables you need to persist between page loads. – Barmar Apr 11 '23 at 15:28

0 Answers0