1

Despite of JavaScript located in ASP.NET Core Page (cshtml), getting always a default message like "Changes Are Made..." from EDGE Browser (or CHROME is the same). Is there a way to use Custom Message?

window.onbeforeunload = function confirmExitPage() {
    event.preventDefault();
    return "My Custom Message I like to Show?";
}

I have already seen similar questions in SO, but none of them supplies a solution...

EDIT

Browsers absolutely prevent User Custom Message, reasonably. Then decide when this compulsory message should be issued to user:

<body>
    <button onclick="activateReloader()">activate</button>
    <button onclick="deactivateReloader()">deactivate</button>
    <script>
        function onBeforeUnload(e) {
            e.preventDefault();
            alert("my custom message");//this not works, for demo only
            e.returnValue = '';
        }

        function activateReloader() {
            window.addEventListener('beforeunload', onBeforeUnload);
        }

        function deactivateReloader() {
            window.removeEventListener('beforeunload', onBeforeUnload);
        }
    </script>
</body>

This code states when message will be displayed to the user and when not.

4
  • Some browsers just don't allow custom messages Commented Sep 24, 2022 at 9:15
  • Window: beforeunload event - Web APIs | MDN: "According to the specification, to show the confirmation dialog an event handler should call preventDefault() on the event." Commented Sep 24, 2022 at 9:22
  • problem is this: alert message is required when a condition exists. Commented Sep 24, 2022 at 9:25
  • @Andreas, thanks, tried event.preventDefault() but still same message Changes you made may not be saved. Commented Sep 24, 2022 at 9:30

1 Answer 1

1

This was prohibited in browsers a long time ago (Chrome 51 is from 2016). You can't show a custom message anymore.

Starting with Firefox 44, Chrome 51, Opera 38, and Safari 9.1, a generic string not under the control of the webpage will be shown instead of the returned string.

https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#compatibility_notes

Reading the issue on the Chromium issue tracker it was about avoiding bad sites using it to try scamming users.

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.

https://bugs.chromium.org/p/chromium/issues/detail?id=587940

Sign up to request clarification or add additional context in comments.

5 Comments

thanks, is it possible to active (and kill) a JavaScript when a special situation arises, this way default message would be meaningful?
Not sure what you are asking. You can run some scripts in beforeunload but you can't show your own dialogs (like alert or prompt) and there is no way to change the default message.
trying to prevent user from leaving page is necessary only when some events occur, so how I activate (or create\kill) onbeforeunload when this event occurs (then I would have no problem with MS Default Message)?
Oh, just don't setup the event before it is needed. Use addEventListener to register the event and then call removeEventListener if you need to remove it again.
thanks, tried it and get what I need, please see my editon on question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.