【发布时间】:2012-04-19 07:01:17
【问题描述】:
我知道在Chrome和FF中,如果window.onbeforeunload返回null,那么对话框就不会弹出了。但在 IE 中,它会弹出消息“null”。为了使 IE 不创建弹出窗口,window.onbeforeunload 应该不返回任何内容。但是在 Chrome 和 FF 中,什么都不返回是否有任何其他副作用?如果不是,为什么会有人费心写'return null;'首先?
例如,这样做:
window.onbeforeunload = function() {
if (shouldNotWarnBeforeUnload)
{ return null; }
else
{ return ('Are you sure you want to leave the page?'); }
return null;
};
还有这个
window.onbeforeunload = function() {
if (shouldNotWarnBeforeUnload)
{ }
else
{ return ('Are you sure you want to leave the page?'); }
};
在 Chrome 中表现不同?
【问题讨论】:
-
不要使用
onbeforeunload,太丑了... -
@c69 这当然很有用:当您不小心离开您的邮件客户端时,您想丢失一份草稿吗?
-
@RobW 如果您是开发人员 - 您最好实现 autosave 而不是烦人的 onbeforeunload 警告,如果您是用户 - 您可以使用更好的浏览器,自动保存表单输入。
-
@c69 即使启用了自动保存,在应用程序完全初始化之前等待几秒钟也是不好的。如果你真的讨厌这个对话框,请使用 Opera,它不支持
onbeforeunload事件。不过是will be implemented in version 12。 -
@RobW 我从 2004 年开始使用 Opera 作为我的主要浏览器;)
标签: javascript internet-explorer firefox google-chrome browser