33

I am testing in Google Chrome.

I did some search and found that someone is using:

window.onbeforeunload = function() {
    if (hook) {
      return "Did you save your stuff?"
    }
  }

But when I use it, I still got the "Changes you made may not be saved." message. How can I change it to something I want?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
AGamePlayer
  • 7,404
  • 19
  • 62
  • 119
  • 2
    This may be a duplicate, but before the June 2, 2016 release, it was possible to have a custom message in the Google Chrome beforeunload event. All of the other questions related to this should include that information. – Bobort Dec 01 '16 at 19:45

2 Answers2

56

You can't, the ability to do this was removed in Chrome 51. It is widely considered a security issue, and most vendors have removed support.

Custom messages in onbeforeunload dialogs (removed):

A window’s onbeforeunload property may be set to a function that returns a string. If the function returns a string, then before unloading the page, a dialog is shown to have the user confirm that they indeed want to navigate away. The string provided by the function will no longer be shown in the dialog. Rather, a generic string not under the control of the webpage will be shown.

Comments

This shipped in Safari 9.1, and has been shipping in Firefox since Firefox 4. Safari considers this a security fix and assigned it CVE-2009-2197 (see https://support.apple.com/en-us/HT206171 ). Approved with the intent https://groups.google.com/a/chromium.org/d/msg/blink-dev/YIH8CoYVGSg/Di7TsljXDQAJ .

Specification

Established standard

Status in Chromium

Removed (launch bug) in:

  • Chrome for desktop release 51
  • Chrome for Android release 51
  • Android WebView release 51
  • Opera release 38
  • Opera for Android release 38
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • 2
    Just wondering, if there's no way to customize it, can I simply use: `window.onbeforeunload = function(e) {};` and `window.onbeforeunload = null;` to activate/deactivate it? – AGamePlayer Nov 23 '16 at 03:43
  • 2
    @AwQiruiGuo Yes, you can still disable it by removing your previous event handlers. – Alexander O'Mara Nov 23 '16 at 05:26
  • You can use window.onbeforeunload = function(e) {} and disable the pop-up in the same time. Just return null at the end of the function: window.onbeforeunload = somefunc; function somefunc() { /*do sth*/ return null;} – MiLAP Jul 14 '21 at 18:25
0

In my Vue 2 application, I was able to use: window.onbeforeunload = null; in mounted() and popup does not open after submitting a form.

Armin
  • 2,021
  • 2
  • 19
  • 17