【问题标题】:Is there any way to perform some operation just after clicking the onbeforeunload confirmation dialog box OK button有没有什么方法可以在点击onbeforeunload确认对话框OK按钮后进行一些操作
【发布时间】:2015-02-12 14:06:52
【问题描述】:

我有以下代码:

var exitHandler=function(){
    return "Are you sure you want to move from this page.";
}

angular.element($window).bind("beforeunload", exitHandler);

当我关闭浏览器选项卡时,它会打开一个弹出确认窗口:

“您确定要卸载此页面吗?”

到目前为止一切正常,但我想执行一些 API 调用,当用户单击 OK 按钮时,而不是 CANCEL 按钮。有没有办法做到这一点?我必须在点击 OK 按钮后执行此操作:

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);
    
                }) 

我希望当 API 调用返回成功时选项卡将关闭,如果返回错误,则选项卡将保持未关闭状态。

【问题讨论】:

标签: javascript jquery angularjs events jquery-events


【解决方案1】:

我个人搜索了很多相同的内容,这是我发现最有帮助的链接。 Is it possible to capture the user's answer when using onbeforeunload confirmation?

正如上面链接中所解释的,只有在按下 OK 确认后才会执行卸载。所以你可以在那里写你的函数。再次运气不好,因为没有保证浏览器将在卸载事件中运行脚本。他们通常不会。

如果你能在不依赖浏览器的情况下完成一些逻辑,那就更好了。

【讨论】:

    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多