javascript onbeforeunload 不显示自定义消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37782104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:43:27  来源:igfitidea点击:

javascript onbeforeunload not showing custom message

javascriptgoogle-chrome

提问by Tushar ani

I have custom message to onbeforeunload event and was working well but I noticed today that it's not showing my message anymore. Instead it shows "changes you made may not be saved"

我有 onbeforeunload 事件的自定义消息并且运行良好,但我今天注意到它不再显示我的消息。相反,它显示“您所做的更改可能无法保存”

window.onbeforeunload = function () {

    return 'Custom message'
}

can anyone please let me know how to fix it?

任何人都可以让我知道如何解决它?

回答by Kaiido

To avoid scamming, chromium and hence chrome have decided to remove the ability to set a custom message in the onbeforeunloaddialog.

为了避免欺诈,chrome 和 chrome 决定删除在onbeforeunload对话框中设置自定义消息的功能。

See this bug reportfrom the 18th of February, 2016.

请参阅2016 年 2 月 18 日的此错误报告

onbeforeunload dialogs are used for two things on the Modern Web:
1. Preventing users from inadvertently losing data.
2. Scamming users.

In an attempt to restrict their use for the latter while not stopping the former, we are going to not display the string provided by the webpage. Instead, we are going to use a generic string.

Firefox already does this[...]

onbeforeunload 对话框在现代 Web 上用于两件事:
1. 防止用户无意中丢失数据。
2. 欺骗用户。

为了在不阻止前者的同时限制它们对后者的使用,我们将不显示网页提供的字符串。相反,我们将使用通用字符串。

Firefox 已经这样做了[...]