onBeforeUnload

Registers a beforeunload event handler for a dashboard page, modal, or plugin extension.

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. For example, this can happen when the site builder selects another page from the dashboard's sidebar, or closes the browser tab. If you use onBeforeUnload in the code for a modal extension, the callback triggers when the site builder 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 site builder that there may be unsaved data. The site builder 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 site builder's browser.

Signature

Copy

Parameters

NameTypeDescription
callbackFunctionCallback 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.

NameTypeDescription
preventDefaultFunctionPrevents the page from unloading. Opens a dialog with a warning that there may be unsaved data on the page.

Returns

Copy

An object containing a function called remove(). Calling this function removes the onBeforeUnload event handler.

Examples

Note: To use this method in self-hosted apps, you need to create a client. See the setup guide for more details.

Prompt for confirmation before unloading unsaved data

Copy
Did this help?