I've read other posts, saying that developers has little to no control over the browser messages when they're about to leave a page, or refresh, I get that. My question is: Is there a way I can listen to what the user chooses when the alert pops up?
My use case is the following: I'm playing a video, if the user wants to leave the page, I want to:
- Pause the video
- Wait for the user to select whether they wanna leave or stay in the page
- If they wanna stay, I want to play the video
- If they decided to leave, I want to send some info to my API
Can anyone help?
For the record, this is what I have, and it certainly pops a message warning the user, but I can't react accordingly tyo user's selection (Leave or Stay)
window.addEventListener("beforeunload", function (e) {
try {
if (currentAction !== 'new' && currentAction !== 'pause' && currentAction !== 'ended') {
_dotnetMainLayoutObjRef.invokeMethodAsync('CloseVideoWhenExitSite')
var confirmationMessage = "\o/";
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
}
} catch (e) {
errorTrack(e.message, "video_actions.js", "beforeunload event in video");
}
});