0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mukund Kumar
  • 21,413
  • 18
  • 59
  • 79
  • can't do what you are asking and there are lots of other posts here about the exact same scenario – charlietfl Feb 12 '15 at 14:13
  • Does this answer your question? [Is it possible to capture the user's answer when using onbeforeunload confirmation?](https://stackoverflow.com/questions/15883127/is-it-possible-to-capture-the-users-answer-when-using-onbeforeunload-confirmati) – Brian Tompsett - 汤莱恩 Mar 05 '22 at 21:51

1 Answers1

1

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.

Community
  • 1
  • 1
paje007
  • 1,001
  • 7
  • 9