我们说我在HTML5中有一个update
页面,如果用户编辑了一个字段,我想在用户退出页面时显示一个弹出窗口(通过点击另一个链接)没有点击update
按钮。弹出窗口将确认用户是否要保存其编辑内容,或者如果用户拒绝,只需继续之前单击的链接即可。如何在HTML5 / JavaScript中实现这一目标?是否有任何预先重定向的功能?感谢。
答案 0 :(得分:2)
使用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>