无法在onbeforeunload事件中自定义消息

Not able to customize message in onbeforeunload event

本文关键字:自定义消息 事件 onbeforeunload      更新时间:2023-09-26

在页面重新加载时我必须显示消息"文件上传正在进行中,您的上传将在重新加载时丢失!"。我使用下面的代码,但警报总是"您所做的更改可能不会被保存!"

window.onbeforeunload = function() {
   return 'File upload in progress, your upload will be lost on reload!';
}

您需要使用文本设置事件的.returnValue属性,而不是返回它。我知道很奇怪,但规格上是这么说的。MDN

window.onbeforeunload = function(event) {
   event.returnValue = 'File upload in progress, your upload will be lost on reload!';
}