Search code examples
javascriptbrowseronbeforeunload

onBeforeUnload suddenly not working as expected


I was using this snippet for almost 2 years. It was fine then, until I saw it was not working today on Google Chrome version 51.0.2704.103m.

$(window).on('beforeunload', function () {
    return "Are you sure you want to exit this page?";
});

It doesn't show my custom message but instead just show:

Changes you made may not be saved.

Is overriding onbeforeunload message is still supported on our browsers? Because this issue is same with Mozilla Firefox version 47.0.1.

Or, am I doing it wrong?


Solution

  • Chrome from Version 51 onwards no longer supports custom messages on beforeunload.

    A window’s onbeforeunload property may be set to a function that returns a string that is shown to the user in a dialog box to confirm that the user wants to navigate away. This was intended to prevent users from losing data during navigation. Unfortunately, it is often used to scam users.

    Details here