beforeunload
beforeunload is a browser event that fires on a window or frame when the document is about to be unloaded, such as during navigation to a different page, a reload, or when the user closes the tab or window. It is intended to give the page a last chance to warn the user about potential data loss.
The event is cancelable. If a listener calls preventDefault or sets a returnValue, many browsers may show
Usage patterns typically involve checking whether the page has unsaved changes and, if so, triggering a prompt.
Limitations and considerations include user experience and interoperability concerns. Because prompts can be disruptive, beforeunload should
Alternatives and best practices focus on proactive data preservation. Where possible, autosave changes or use the