Javascript 为什么jquery beforeunload即使在Internet Explorer中返回null也会显示消息
此代码将导致Internet Explorer每次都显示“是否确实要离开?”消息,即使dirtyMessage为空。我做错了什么Javascript 为什么jquery beforeunload即使在Internet Explorer中返回null也会显示消息,javascript,jquery,onbeforeunload,Javascript,Jquery,Onbeforeunload,此代码将导致Internet Explorer每次都显示“是否确实要离开?”消息,即使dirtyMessage为空。我做错了什么 $(window).bind('beforeunload', function() { var dirty = didUserUpdateSomething(); if (dirty) { return "You will lose your changes."; } return null; }); 显然返回null是问题所在。适用于
$(window).bind('beforeunload', function() {
var dirty = didUserUpdateSomething();
if (dirty) {
return "You will lose your changes.";
}
return null;
});
显然返回null是问题所在。适用于除旧IE以外的所有浏览器。解决方案是不返回任何内容(未定义):
显然,返回null是个问题。适用于除旧IE以外的所有浏览器。解决方案是不返回任何内容(未定义):
IE中的
卸载前有点古怪;返回事件处理程序中的任何内容(即使为null)都会导致事件处理程序弹出一个框,询问用户是否确定要离开页面
您可以尝试以下解决方法:
var beforeUnloadFired = false;
$(window).bind('beforeunload', function (event) {
if (!beforeUnloadFired) {
beforeUnloadFired = true;
event.returnValue = "You have attempted to leave this page. Are you sure you want to navigate away from this page?";
}
window.setTimeout(function () {
ResetUnloadFired();
}, 10);
});
function ResetUnloadFired() {
beforeUnloadFired = false;
}
两份参考资料:
及
注:关于第二个参考中的相关信息,请参见“备注”。IE中卸载前的有点古怪;返回事件处理程序中的任何内容(即使为null)都会导致事件处理程序弹出一个框,询问用户是否确定要离开页面
您可以尝试以下解决方法:
var beforeUnloadFired = false;
$(window).bind('beforeunload', function (event) {
if (!beforeUnloadFired) {
beforeUnloadFired = true;
event.returnValue = "You have attempted to leave this page. Are you sure you want to navigate away from this page?";
}
window.setTimeout(function () {
ResetUnloadFired();
}, 10);
});
function ResetUnloadFired() {
beforeUnloadFired = false;
}
两份参考资料:
及
注意:请参阅“备注”以获取第二个参考中的相关信息。请随意重构您的didUserUpdateMething()
functionFeel可以为didUserUpdateMething()进行重构代码>功能