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!