此代码将使 Internet Explorer 显示“您确定要离开吗?”每次都会收到消息,即使 dirtyMessage 为 null 时也是如此。我做错了什么?
$(window).bind('beforeunload', function() {
var dirty = didUserUpdateSomething();
if (dirty) {
return "You will lose your changes.";
}
return null;
});
最佳答案
显然返回 null 是问题所在。适用于除旧版 IE 之外的所有其他浏览器。解决方案是不返回任何内容(未定义):
$(window).bind('beforeunload', function() {
var dirty = didUserUpdateSomething();
if (dirty) {
return "You will lose your changes.";
}
// notice that in case of no dirty message we do not return anything.
// having 'return null;' will make IE show a popup with 'null'.
});
关于javascript - 为什么即使在 Internet Explorer 中返回 null,jquery beforeunload 也会显示一条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16528365/