55

I have custom message to onbeforeunload event and was working well but I noticed today that it's not showing my message anymore. Instead it shows "changes you made may not be saved"

window.onbeforeunload = function () {

    return 'Custom message'
}

can anyone please let me know how to fix it?

Tushar ani
  • 600
  • 1
  • 5
  • 11
  • Related/duplicate: [How can I override the OnBeforeUnload dialog and replace it with my own?](https://stackoverflow.com/q/276660) – Makyen Jul 31 '18 at 05:44

1 Answers1

96

To avoid scamming, chromium and hence chrome have decided to remove the ability to set a custom message in the onbeforeunload dialog.

See this bug report from the 18th of February, 2016.

onbeforeunload dialogs are used for two things on the Modern Web:
1. Preventing users from inadvertently losing data.
2. Scamming users.

In an attempt to restrict their use for the latter while not stopping the former, we are going to not display the string provided by the webpage. Instead, we are going to use a generic string.

Firefox already does this[...]

Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • Is there a workaround to show a custom message to the user?\ – R.S.K Aug 22 '16 at 20:09
  • 18
    @R.S.K no, that would defeat the reason why they did it. – Kevin Wiskia Nov 07 '16 at 22:06
  • 6
    I'm so glad they finally did this. One less trick for leech parasites on the web. – John Weisz Aug 01 '17 at 13:19
  • 6
    yeah but unfortunately those who abused it once again screw those who didn't abuse it... it'd be nicer if it said: "You have unsaved changes, are you sure you want to leave the current page" or something to that tune. – Dylan Hayes Sep 21 '18 at 17:09
  • I agree.. the scammers have crapped on us.. I want to warn my user when they leave close my site, if it's offline. – baash05 Nov 02 '18 at 03:39
  • @baash05 you can catch user's mouse scrolling over top of the page and show the warning - https://github.com/carlsednaoui/ouibounce – eggnukes Jan 10 '19 at 12:12