1

我放了以下脚本以防止离开页面同时处理步骤

<script language="JavaScript">
window.onbeforeunload = confirmExit;

function confirmExit()
{
JQObj.ajax({
    type: "POST",
    url: "<?php echo $this->url(array('controller'=>'question','action'=>'cleaSess'), 'default', true); ?>",
    success: function(data){}
});

return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
}

</script>

但是每次我收到默认警报消息而不是设置自定义警报消息时,

我想在最终用户单击“离开页面”按钮时调用 ajax 调用,但是在上面的脚本中,在单击离开按钮之前调用 ajax 调用,

当且仅当人们离开页面时,任何人都有想法或逻辑来调用 ajax。

4

2 回答 2

2

您可以使用“卸载”事件来发送 AJAX 请求:

<script type="text/javascript">
    window.onbeforeunload = function() {
        return "You have attempted to leave this page. "
               + "If you have made any changes to the fields without "
               + "clicking the Save button, your changes will be lost. "
               + "Are you sure you want to exit this page?";
    };

    window.onunload = function() {
        // Ending up here means that the user chose to leave
        JQObj.ajax({
            type: "POST",
            url: "http://your.url.goes/here",
            success: function() {}
        });
    };
</script>

另请参阅这个简短的演示

于 2013-10-21T19:02:08.753 回答
0

你应该尝试这样的事情:

window.onbeforeunload = displayConfirm;

function displayConfirm()
{
    if (confirm("You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?"))
    {
        confirmExit();
    }
}
于 2012-10-15T05:53:26.827 回答