> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt ## Resource: onBeforeUnload() ## Article: onBeforeUnload() ## Article Link: https://dev.wix.com/docs/sdk/host-modules/dashboard/on-before-unload.md ## Article Content: # onBeforeUnload() Registers a [`beforeunload` event](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event) handler for a dashboard page, modal, or plugin [extension](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/dashboard-extensions/about-dashboard-extensions.md). `onBeforeUnload()` accepts a callback that's triggered when a Wix users is about to navigate away from a dashboard page or when the browsing context of an app is being unloaded. For example, this can happen when the Wix user selects another page from the dashboard's sidebar, or closes the browser tab. If you call `onBeforeUnload` in the code for a [modal extension](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/dashboard-extensions/dashboard-modals/about-dashboard-modals.md), the callback triggers when the Wix user is about to close the modal. The event object passed to the callback function contains a function called `preventDefault()`. If the callback calls `preventDefault()`, navigation away from the page or other context unloading is paused. A dialog is displayed in the dashboard warning the Wix user that there may be unsaved data. The Wix user can then choose to cancel and stay in the current context or proceed. > **Note:** You should not assume that the `beforeunload` event will always fire or that the confirmation dialog will always be presented. These behaviors vary depending on the Wix user's browser. ## Method Declaration ```ts (callback: (event: Event) => void) => { remove: () => void; }; ``` ## Parameters | Name | Type | Description | |:-----------|:-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `callback` | Function | Callback function to call when the `beforeunload` event fires. The function receives an `event` object as an argument. | ### Event object The `event` object passed to the callback function. |Name|Type|Description| |:--|:--|:--| |`preventDefault`| Function | Prevents the page from unloading. Opens a dialog with a warning that there may be unsaved data on the page. ## Returns ```ts { remove: () => void; } ``` An object containing a method called `remove()`. Calling this method removes the `onBeforeUnload` event handler. ## Examples > **Note:** To call this method in [self-hosted apps](https://dev.wix.com/docs/sdk/articles/get-started/about-self-hosted-apps.md), you need to create a [client](https://dev.wix.com/docs/sdk/articles/set-up-a-client/about-the-wix-client.md). See the [setup](https://dev.wix.com/docs/sdk/host-modules/dashboard/introduction.md) guide for more details. ### Prompt for confirmation before unloading unsaved data ```ts import { dashboard } from '@wix/dashboard'; const { remove } = dashboard.onBeforeUnload((event) => { // Check if there's unsaved data on the page if (unsavedPageData) { event.preventDefault(); } }); ```