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

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

问题描述

当使用 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 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?

    解决方案

    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 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天全站免登陆