[whatwg/dom] Suggestion: introduce window.beforeunloadwarning, deprecate window.onbeforeunload (#929)

Currently, the only way to prevent (possibly accidentally) closing a tab or window with unsaved content is to handle event 'beforeunload` and return a warning string (or set event.returnValue). This is problematic because the browser doesn't know before executing this syncronous method if there's need to possibly abort the process of switching document OR close the tab.

I suggest DOM spec should instead define string window.beforeUnloadWarning where the behavior is to prevent closing a tab or window if the value of that string is non-empty string. If the value is non-empty string, the string will contain localized warning message / reason to show to the user as a reason to keep the tab or window open.

Rationale:
- This would allow UA to display UA specific UI elements for tabs that have unsaved content. For example, many Windows applications show a star in tab if it contains unsaved data. UA could display the marker immediately the string beforeUnloadWarning is set instead of waiting until the end user tries to close the tab or window before calling the event handler to check if such a warning is actually required.
- This would allow UA to know that it's okay to just close the tab or window without running any events. As a result, the response time to close a tab or window or to navigate to another page would be improved because there's no need to run any syncronous code. (Obviously, this requires that legacy code has not set any `beforeunload` handlers.)

Things to consider / decide:
- Feature detect: the value window.beforeUnloadWarning should always exists and have value `""` (empty string) by default.
- Error handling: should running window.beforeUnloadWarning = false (or null) silently set the value to empty string or throw an exception? I'm thinking setting the value to empty string would be okay.
- Is there need for multiple warnings at once? The API `addEventListener('beforeunload')` allows setting multiple independent handlers. One can imagine use cases where multiple warnings would make sense, e.g. "Saving changes to settings still in progress." and "Uploading photo in the background is still in progress."
- The spec would need to suggest some kind of wording or context for the error message - for example, is the warning message expected to start with "Warning:", is the warning message expected to describe the current state (e.g. "saving changes") or declare the unsaved data (e.g. "Photo upload in the background") or something else?

If the future intent is to prevent setting any user visible strings in the beforeunload API, the value of the above value could be `int` and the expected use case is to execute `window.beforeUnloadWarning++` when starting something that should be completed before closing the current document and to execute `window.beforeUnloadWarning--` when ready.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/929

Received on Monday, 23 November 2020 15:35:38 UTC