I would like to run function if user click on 'Leave Page'. This function will send an AJAX call. In order for this function to be executed browser has to remain open. SO I'm wondering how I can close the window once this function is done/completed? Here is example that I have:
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Your browser is now offline.",
recID = $.trim($("#recID").val());
removeUnlock(recID); // Call function to remove record from the Locked table.
$("#unlockBtn").prop("disabled",true); //Set Unlock button attribute to disabled.
$('.blockEdit').prop("disabled",true); //Set Edit button attribute to disabled.
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
console.log(confirmationMessage);
return confirmationMessage; //Webkit, Safari, Chrome
});
I have updated my code example. I found the way to call unlock function but there is one problem. There is no way to check if user picked 'Leave' or 'Stay' on the page. Unlock function should run only if user picks to leave the page. In that case like I mentioned there is no enough time to process ajax call. I'm not sure that question marked as duplicate explains or have the answer to this situation...