gpt4 book ai didi

javascript - 无法在页面离开 "onbeforeunload"事件中获取自定义消息警报

转载 作者:行者123 更新时间:2023-11-30 18:17:34 25 4
gpt4 key购买 nike

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

<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。

最佳答案

您可以使用“unload”事件发送 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>

另请参阅此 short demo .

关于javascript - 无法在页面离开 "onbeforeunload"事件中获取自定义消息警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12889955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com