Issue
I have the following code:
var exitHandler=function(){
return "Are you sure you want to move from this page.";
}
angular.element($window).bind("beforeunload", exitHandler);
When I close the browser tab it opens a popup confirmation:
"Are you sure you want to unload this page ?"
Up till here it is ok, but I want to perform some API call, when the user clicks on the OK button, but not on the CANCEL button. Is there any way to do this? I have to perform this operation just after clicking on the OK button:
Participant.setDorateStatus({id:localData.userId},{dorate2status:true})
.$promise.then(function(data) {
console.log(data)
endExperiment=data;
})
.catch(function(err) {
if (err.status != 401)
$scope.addNotification("something went wrong.", "danger", 5000);
})
and I want when the API call returns success then the tab will close, if it will return error the tab will remain unclosed.
Solution
I personally searched a lot about the same and this is the link that I found most helpful. Is it possible to capture the user's answer when using onbeforeunload confirmation?
As it is explained in the link above, unload executes only after the OK confirmation is pressed. So you could write your function there. Again it is a tough luck because there is no guaratee that the browsers will run scripts in the unload event. They don't usually.
Some logic if you can work out without depending on the browser, it is better.
Answered By - paje007
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.