What the person is doing
The task includes typed answers, selected records, uploads, payment setup, edited content, in-progress review, temporary calculations, or session-bound state.
Application · lesson 10 of 23 · Error Prevention And Recovery
Underlying principle: Recovery begins before the error. Clear consequences, suitable constraints, preserved work, and a visible correction path reduce the cost of a mistake.
Track departure-risk state precisely, attach browser leave protection only while needed, intercept in-app navigation with a clear decision surface, identify what is at risk, offer save, discard, and stay choices where possible, and clear the warning immediately after work is safe.
Recovery begins before the error. Clear consequences, suitable constraints, preserved work, and a visible correction path reduce the cost of a mistake.
The task includes typed answers, selected records, uploads, payment setup, edited content, in-progress review, temporary calculations, or session-bound state.
Track departure-risk state precisely, attach browser leave protection only while needed, intercept in-app navigation with a clear decision surface, identify what is at risk, offer save, discard, and stay choices where possible, and clear the warning immediately after work is safe.
Dirty unsaved state with local unsaved-work status.
What exact work or context would be lost if the user leaves now?
The warning appears only after a departure attempt or immediately before a known session boundary that could cause loss.
The task includes typed answers, selected records, uploads, payment setup, edited content, in-progress review, temporary calculations, or session-bound state.
Open the working example, try its controls, and compare the behavior against the decision guidance.
The task includes typed answers, selected records, uploads, payment setup, edited content, in-progress review, temporary calculations, or session-bound state.
Track departure-risk state precisely, attach browser leave protection only while needed, intercept in-app navigation with a clear decision surface, identify what is at risk, offer save, discard, and stay choices where possible, and clear the warning immediately after work is safe.
Dirty unsaved state with local unsaved-work status.
The warning appears after every click even when the page is clean.
Use a real dialog or alertdialog for in-app warnings, with a heading that names the loss risk.
Mozilla - checked
MDN documents beforeunload for preventing unsaved data loss and recommends conditional use.
web.dev - checked
web.dev documents browser navigation-cache considerations and recommends limiting unload-related handlers.
World Wide Web Consortium - checked
W3C documents warning users before timeouts can cause data loss.
World Wide Web Consortium - checked
W3C documents preserving user activity after re-authentication.
Source trail checked: