我需要在用户离开页面之前警告用户未保存的更改(这是一个非常常见的问题).
window.onbeforeunload=handler
Run Code Online (Sandbox Code Playgroud)
这有效,但它会引发一个默认对话框,其中包含一条包含我自己文本的令人生气的标准消息.我需要完全替换标准消息,所以我的文本是清楚的,或者(更好)用一个模式对话框替换整个对话框使用jQuery.
到目前为止,我失败了,我还没有找到任何似乎有答案的人.它甚至可能吗?
我的页面中的Javascript:
<script type="text/javascript">
window.onbeforeunload=closeIt;
</script>
Run Code Online (Sandbox Code Playgroud)
closeIt()函数:
function closeIt()
{
if (changes == "true" || files == "true")
{
return "Here you can append a custom message to the default dialog.";
}
}
Run Code Online (Sandbox Code Playgroud)
使用jQuery和jqModal我尝试过这种事情(使用自定义确认对话框):
$(window).beforeunload(function() {
confirm('new message: ' + this.href + ' !', this.href);
return false;
});
Run Code Online (Sandbox Code Playgroud)
这也行不通 - 我似乎无法绑定到beforeunload事件.