0

I have implemented beforeunload function in my single page app. This seems very cool because we can check when user wants to close their browser/window. This was very important in my app. App has locking feature that prevents multiple users editing same record at the same time. However this cause some other problems like user simply closing their browser and record would stay locked in that case. So after I implemented beforeunload every time user click's on X to close their browser or window function to unlock the record will run. Everything seemed fine until I tried to use Logoff button in my app. This cause the problem. As soon as click on the logoff button my unlock function will run then beforeunload will be triggered and function will run second time using same recordID. I tried to ignore beforeunload once user clicks on the Logoff but that didn't work since Logoff is in the separate iframe. I was wondering if there is a way to check recID every time before my unlock function runs. If ID is the same as the previous ID I should skip and not run the function. Here is example of my function:

window.onbeforeunload = function () {
    var recID = $.trim($("#recordID").val());
    //Here I should check if recID is the same or different. If function runs more than once recID always should be compared with the previous.
    if(recID){
        removeUnlock(recID); //Call function to remove record.
    }

    return 'Are you sure you want to leave?';
}

If anyone knows how this can be achieved or better way to handle this situation please let me know. Thank you!

espresso_coffee
  • 5,076
  • 9
  • 53
  • 124
  • `App has locking feature that prevents multiple users editing same record at the same time. ` You should not be handling locking logic in the browser land. – Keith Dec 15 '17 at 17:06
  • @Keith How I should handle that? I have server side validation but still I need front end validation. Also users should be aware when someone else is currently working on that record. Also I'm not sure how I can detect if user close their browser on the back end... – espresso_coffee Dec 15 '17 at 17:08
  • Well it depends on your app. But locking in browser is wrong. We used to use record locking, but I now use a none locking record editing. I basically track changes to records, rather than a whole record buffer, and save the changes. Nice thing here is that 2 people can edit the same record without problems. If 2 people alter the same field, last wins, and this is what would happen in a locking system anyway.. :) – Keith Dec 15 '17 at 17:16
  • If not using a none locking record editing system is totally out the question. Another solution, don't use HTTP protocol, as this is stateless. Handle record locking say using Sockets, this is then a state protocol, IOW: at the server side you can detect lost / closed connections. – Keith Dec 15 '17 at 17:26

1 Answers1

0

It is possible to communicate between iframes with messages How to communicate between iframe and the parent site? or through session storage if your pages are in the same domain sessionStorage in iframe.

So you should probably send a message from log off iframe if logout went correctly and save this information while listening to those event in your code. Then add additional condition in onbeforeunload.

jkordas
  • 155
  • 6