Search.../

onBeforeUnload( )

Registers a beforeunload event handler for a dashboard page.

Description

This function can only be used in page code files for dashboard pages created in the Wix Editor or with Wix Blocks.

onBeforeUnload() accepts a callback that's triggered when a site builder is about to navigate away from a dashboard page or when the browsing context of an app is being unloaded. This can happen when the site builder selects another page from the dashboard's sidebar.

The event object passed to the callback function contains a function called preventDefault(). If the callback calls preventDefault(), navigation away from the page is paused. A dialog is displayed in the dashboard warning the site builder that there may be unsaved data on the page. The site builder can choose to stay on the current page and save their data, or to proceed.

Notes:

  • The beforeunload event does not fire when a site builder closes the browser or refreshes the page.
  • 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 site builder's browser.

Syntax

function onBeforeUnload(callback: onBeforeUnloadCallback): onBeforeUnloadReturn
callback: function onBeforeUnloadCallback(event: onBeforeUnloadEvent): void

onBeforeUnload Parameters

NAME
TYPE
DESCRIPTION
callback

Callback function to call when the beforeunload event fires. The function receives an event object as an argument.

Returns

Return Type:

onBeforeUnloadReturn
NAME
TYPE
DESCRIPTION
remove
Function

Removes the onBeforeUnload event handler.

onBeforeUnloadCallback Parameters

NAME
TYPE
DESCRIPTION
event
onBeforeUnloadEvent

Event object. Contains a function called preventDefault().

Returns

This function does not return anything.

Return Type:

void

Was this helpful?

Ask for confirmation before unloading unsaved data

Copy Code
1import { onBeforeUnload } from 'wix-dashboard';
2
3// ...
4
5const { remove } = onBeforeUnload((event) => {
6 // Check if there is unsaved data on the page
7 if (unsavedPageData) {
8 event.preventDefault();
9 }
10});
11
12// ...
13
14// Remove the onBeforeUnload event handler
15remove();