是否可以在 beforeunload 弹出窗口中显示自定义消息? [英] Is it possible to display a custom message in the beforeunload popup?

查看:50
本文介绍了是否可以在 beforeunload 弹出窗口中显示自定义消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

通过查看现有答案,我感觉过去使用 confirmalertevent.returnValue 之类的东西是可能的,但现在他们似乎不再工作了.

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

解决方案

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

<块引用>

快速说明(因为这是一个旧答案) - 现在所有主要浏览器都不支持 beforeunload 弹出窗口中的自定义消息.没有新的方法可以做到这一点.如果您仍然需要支持旧浏览器 - 您可以在下面找到信息.

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

jQuery

$(window).bind("beforeunload",function(event) {return "您有一些未保存的更改";});

Javascript

window.onbeforeunload = function() {return "离开此页面将重置向导";};

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


<块引用>

还有一些注意事项:

<块引用>

  1. 并非所有浏览器都支持此功能(

    火狐:

    Safari:

    IE:

    <块引用>

    只是为了确保 - 你需要包含 jquery

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

    1. Chrome 删除版本 51 中对自定义消息的支持
    2. Opera 删除版本 38 中对自定义消息的支持
    3. Firefox 在 44.0 版中移除了对自定义消息的支持(仍在寻找此信息的来源)
    4. 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 or alert or event.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?

    解决方案

    tl;dr - You can't set custom message anymore in most modern browsers

    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.

    In order to set a confirmation message before the user is closing the window you can use

    jQuery

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

    Javascript

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

          It's important to notice that you can't put 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.

    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:

    1. Chrome removed support for custom message in ver 51
    2. Opera removed support for custom message in ver 38
    3. Firefox removed support for custom message in ver 44.0 (still looking for source for this information)
    4. Safari removed support for custom message in ver 9.1

    这篇关于是否可以在 beforeunload 弹出窗口中显示自定义消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆