gpt4 book ai didi

javascript - 是否可以在 beforeunload 弹出窗口中显示自定义消息?

转载 作者:IT王子 更新时间:2023-10-29 03:05:46 25 4
gpt4 key购买 nike

当使用 window.onbeforeunload(或 $(window).on("beforeunload"))时,是否可以在该弹出窗口中显示自定义消息?

也许是适用于主流浏览器的小技巧?

通过查看现有的答案,我觉得这在过去使用诸如 confirmalertevent.returnValue 之类的东西是可能的,但现在它们似乎不再起作用了。

那么,如何在 beforeunload 弹出窗口中显示自定义消息?这甚至/仍然可能吗?

最佳答案

tl;dr - 您不能再在大多数现代浏览器中设置自定义消息

A quick note (since this is an old answer) - these days all major browsers don't support custom message in the beforeunload popup. There is no new way to do this. In case you still do need to support old browsers - you can find the information below.

为了在用户关闭您可以使用的窗口之前设置确认消息

jQuery

$(window).bind("beforeunload",function(event) {
return "You have some unsaved changes";
});

Javascript

window.onbeforeunload = function() {
return "Leaving this page will reset the wizard";
};

请务必注意,您不能放置 confirm/alert inside beforeunload


A few more notes:

  1. NOT all browsers support this (more info in the Browser compatibility section on MDN)2. In Firefox you MUST do some real interaction with the page in order for this message to appear to the user.
    3. Each browser can add his own text to your message.

以下是使用我可以访问的浏览器的结果:

Chrome:

Chrome alert on exit

火狐:

Firefox alert on exit

Safari :

Safar alert on exit

IE:

IE alert on exit

Just to make sure - you need to have jquery included

有关浏览器支持和删除自定义消息的更多信息:

  1. Chrome removed在版本 51 中支持自定义消息
  2. 歌剧 removed在版本 38 中支持自定义消息
  3. Firefox 在版本 44.0 中删除了对自定义消息的支持(仍在寻找此信息的来源)
  4. Safari removed支持 9.1 版中的自定义消息

关于javascript - 是否可以在 beforeunload 弹出窗口中显示自定义消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com