我正在创建一个HTML布局,用户可以通过使用“save”和“save as draft”resp来填充数据并以HTML和JSON格式保存该页面。我可以用
吗?window.onbeforeunload
在我的template.html
文件中两次显示两条不同的警报消息?
警报会显示:
答案 0 :(得分:1)
首先看{4}来了解如果你使用它两次会发生什么,那么下面给出的想法
window.onbeforeunload=function()
{
if(form_empty())
return "Form is empty"; // message for empty form
else
return "Form is not empty"; // message for filled up form
}
function form_empty()
{
// check form fields whether the form is empty or not
// return true if form is empty
// return false if form is not empty
}
答案 1 :(得分:0)
就像以这种方式分配的任何其他事件处理程序一样,如果再次设置它,它将销毁已存在的任何处理程序。
现在,如果您根据页面状态添加和删除事件,那么这应该不是问题。否则,它会有点复杂。但是,在onbeforeunload
的情况下,最好只有一个。