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.
Advertisement
Answer
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:
- Preventing users from inadvertently losing data.
- 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