是否有可能在beforeunload弹出窗口中显示自定义消息? [英] Is it possible to display a custom message in the beforeunload popup?
问题描述
window.onbeforeunload
(或 $(window).on(beforeonload)
)时,有可能在弹出窗口中显示自定义消息吗?也许一个小技巧可以在主流浏览器上使用吗?
通过查看现有的答案,我有这样的感觉,在过去使用诸如 confirm
或 alert
或 event.returnValue
,但是现在看起来他们已经不能工作了。
因此,如何显示自定义消息beforeunload popup?这是甚至还是可能的?
为了在用户关闭窗口之前设置确认消息,可以使用
$(window).bind(beforeunload,function(event){
return ;
});
    请注意,您 无法 确认/提醒
里面 beforeunload
<所有浏览器都支持这个功能(更多信息请参阅
Firefox: a href =https://i.stack.imgur.com/rpxhz.png>
Safari浏览器:
IE: stack.imgur.com/zC5Ys.png\">
只要确保 - 你需要有jquery incl
有关浏览器支持和删除自定义消息的更多信息:
$ b $
Chrome浏览器删除支持自定义消息 支持自定义消息在版本44.0(仍然在寻找这个信息的来源)
Safari 删除对版本9.1中的自定义消息的支持
When using
window.onbeforeunload
(or$(window).on("beforeonload")
), is it possible to display a custom message in that popup?Maybe a small trick that works on major browsers?
By looking at existing answers I have the feeling this was possible in the past using things like
confirm
oralert
orevent.returnValue
, but now it seems they are not working anymore.So, how to display a custom message in the beforeunload popup? Is that even/still possible?
解决方案In order to set a confirmation message before the user is closing the window you can use
$(window).bind("beforeunload",function(event) { return "You have some unsaved changes"; });
It's important to notice that you can't put
confirm/alert
insidebeforeunload
A few more notes:
- NOT all browsers support this (more info in the Browser compatibility section on MDN)
- In Firefox you MUST do some real interaction with the page in order for this message to appear to the user.
- Each browser can add his own text to your message.
Here are the results using the browsers I have access to:
Chrome:
Firefox:
Safari:
IE:
Just to make sure - you need to have jquery included
More information regarding the browsers support and the removal of the custom message:
- Chrome removed support for custom message in ver 51
- Opera removed support for custom message in ver 38
- Firefox removed support for custom message in ver 44.0 (still looking for source for this information)
- Safari removed support for custom message in ver 9.1
这篇关于是否有可能在beforeunload弹出窗口中显示自定义消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!