是否可以在卸载前弹出窗口中显示自定义消息?使用时window.onbeforeunload(或$(window).on("beforeonload")),是否可以在弹出窗口中显示自定义消息?也许是在主要浏览器上起作用的小把戏?通过查看现有的答案,我觉得这在过去是有可能的,比如confirm或alert或event.returnValue但现在他们似乎不再工作了。那么,如何在卸载前弹出窗口中显示自定义消息?这是可能的吗?
3 回答
郎朗坤
TA贡献1921条经验 获得超9个赞
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 内 beforeunload
还有几个注意事项:
不
所有浏览器都支持这一点(更多信息在 (关于MDN的一节) 在Firefox中,您必须与页面进行一些真正的交互,这样才能使用户看到此消息。 每个浏览器都可以将自己的文本添加到您的邮件中。
为了确保-您需要包含jQuery
侃侃尔雅
TA贡献1801条经验 获得超16个赞
使用时 window.onbeforeunload(或 $(window).on("beforeonload")),是否可以在弹出窗口中显示自定义消息?
通过查看现有的答案,我觉得这在过去是有可能的,比如 confirm或 alert或 event.returnValue但现在他们似乎不再工作了。
confirmalertonbeforeunload
onreturnValue
$(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};添加回答
举报
0/150
提交
取消
