Home  >  Q&A  >  body text

Is it possible to display a custom message in the pre-uninstall popup?

<p>When using <code>window.onbeforeunload</code> (or <code>$(window).on("beforeunload")</code>), is it possible to display the self in the pop-up window? Define message? < /p> <p>Maybe a little trick for the major browsers? </p> <p>Looking at the existing answers, I think in the past using something like <code>confirm</code> or <code>alert</code> or <code>event.returnValue</code> Things were possible, but now it seems they no longer work. </p> <p>So, how to display custom message in beforeunload popup? Is this even/still possible? </p>
P粉478445671P粉478445671392 days ago450

reply all(2)I'll reply

  • P粉186904731

    P粉1869047312023-08-25 00:34:09

    Not anymore. All major browsers started ignoring the actual message and only showing their own.

    correct. A long time ago you could use confirm or alert, more recently you could return a string handler from onbeforeunload and the string would be shown. The contents of the string are now ignored and treated as flags.

    When using jQuery's on you do have to use returnValue on the original event:

    $(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";
    });

    Or the old fashioned way:

    window.onbeforeunload = function() {
        return "Your message here"; // Probably won't be shown, see note above
    };

    That's all you can do.

    reply
    0
  • P粉143640496

    P粉1436404962023-08-25 00:32:48

    tl;dr - You can no longer set a custom message in most modern browsers

    In order to set a confirmation message before the user closes 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 is important to note that you cannot place confirmation/alert internal beforeunload< /代码>


    Here are the results using a browser I have access to:

    Chrome:

    Firefox browser:

    Safari:

    IE:

    For more information on browser support and removal of custom messages:

    1. Chrome Removed Support for custom messages
    2. in version 51
    3. Opera Removed in version 38 Support for custom messages
    4. Firefox removed support for custom messages in version 44.0 (still looking for source of this information)
    5. Safari Removed in version 9.1 Support for custom messages

    reply
    0
  • Cancelreply