IE 11上的beforeunload - 不提示用户不起作用 [英] beforeunload on IE 11 - do not prompt user does not work

查看:546
本文介绍了IE 11上的beforeunload - 不提示用户不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将侦听器添加到 beforeunload 事件的全局窗口对象时,IE 11(和10)的行为与Chrome和Firefox不同。

When adding a listener to the global window object for the beforeunload event, IE 11 (and 10) does not behave as Chrome and Firefox.

通常,您返回一个字符串,用于填充浏览器本地对话框提示符,如果您不希望对话框提示用户,则返回空字符串。

Normally, you return a string that will be used to populate the browser-native dialog prompt or you return an empty string if you do not want the dialog to prompt the user.

但是,在IE 11中,如果返回空字符串和/或将 evt.returnValue 设置为空字符串,则浏览器-native'Navigate Away'对话框打开并提示用户确认他们可能会丢失未保存的更改。

However, in IE 11, if you return an empty string and/or set the evt.returnValue to an empty string, the browser-native 'Navigate Away' dialog is opened and prompts the user to acknowledge that they may lose unsaved changes.

有没有办法(无需删除事件监听器) IE 11中没有出现对话框?

Is there any way (without having to remove the event listener) to have the dialog not appear in IE 11?

请参阅我的 JSFiddle (在IE中 - 因为它应该可以在Chrome,Firefox和Safari中正常工作)。

See my JSFiddle (in IE - as this should work properly in Chrome, Firefox, and Safari).

以下是小提琴中的来源:

Here's the source in the fiddle:

var isDirty = false;
var message = '** You have unsaved changes. **'
window.addEventListener('beforeunload', function(evt){
  if(isDirty) {
    evt.returnValue = message;
    return message;
  }
  delete  evt.returnValue;
  return "";
});


推荐答案

解决办法是不返回任何东西(这是与 return相同; 返回undefined; )。

The solution is not to return anything (which is the same as return; or return undefined;).

var isDirty = false;
var message = '** You have unsaved changes. **'
window.addEventListener('beforeunload', function(evt){
  if(isDirty) {
    evt.returnValue = message;
    return message;
  }
  delete evt.returnValue;
});

这篇关于IE 11上的beforeunload - 不提示用户不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆