假设我有一个 HTML5 中的 update
页面,如果用户编辑了一个字段,我想在用户退出页面(通过单击另一个链接)时显示一个弹出窗口,而无需单击更新
按钮。弹出窗口将确认用户是否想要保存他的编辑,或者如果用户拒绝,只需继续他之前单击的链接即可。如何在 HTML5/JavaScript 中实现这一点?有没有抢占重定向的功能?谢谢。
最佳答案
使用onbeforeunload
事件。
查看演示:onbeforeunload Demo
JS代码:
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
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>
关于javascript - 通过弹出窗口抢占移动到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26647584/