使用window.onbeforeunload(或$(window).on("beforeunload"))时,是否可以在该弹出窗口中显示自定义消息?< /p>
也许是一个适用于主要浏览器的小技巧?
通过查看现有的答案,我觉得过去使用诸如 confirm 或 alert 或 event.returnValue 之类的东西是可能的,但现在看来他们不再工作了。
那么,如何在 beforeunload 弹出窗口中显示自定义消息?这甚至/仍然可能吗?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
不再是了。所有主要浏览器都开始忽略实际消息并只显示自己的消息。
正确。 很久以前,您可以使用
confirm或alert,最近您可以从onbeforeunload返回一个字符串处理程序并且该字符串将被显示。现在,字符串的内容将被忽略,并将其视为标志。当使用 jQuery 的
on时,您确实必须在原始事件上使用returnValue:$(window).on("beforeunload", function(e) { // Your message won't get displayed by modern browsers; the browser's built-in // one will be instead. But for older browsers, best to include an actual // message instead of just "x" or similar. return e.originalEvent.returnValue = "Your message here"; });或者老式的方式:
window.onbeforeunload = function() { return "Your message here"; // Probably won't be shown, see note above };这就是你所能做的。