Set up window eventListener to listen for page unload

Hi, I'm trying to set up a general window event listener, specifically, the 'before unload' event from the page lifecycle API.

Question: Is there any way to set general listeners in a retool dashboard -- setting JS that isn't triggered by a UI action or on pageLoad.

My use case is just generating a warning modal if the user exits their tab or browser.

Thank you!

P.S. Hi Victoria / best TA ever!!! :smiley:

1 Like

Hello mysterious @alw93!

We don't have access to window event handlers, like onbeforeunload, since all JS in Retool runs in a sandbox. 😓

1 Like

oof, okay will have to think of another solution then :slight_smile: ty!

Hi @victoria

is there a way with retool to prompt a modal if there is any unsaved change when leaving the app. (say the app has a form that needs inputs and saving)

1 Like

Hi Philippe! If there was an event to watch for app closing, then we could trigger a modal to open based on 1. this ideal, but non-existent app closing event and 2. !textInput1.value (or some way to see if a form input is empty or not).

If you throw your form into a modal, you could watch for the modal closing and check if any components are empty. If empty, you could trigger the same modal to open (or trigger a notification to show, or open a new modal) with something like this event handler:

Would something like this work for you?

Hello @victoria, Still can't use addEventListener?

we tried to use resize event but apparently doesn't work.

thank you!

EDIT: i think i found the posible solution i created a question but i added a comment with the posible solution, let me know if i can use the events :slight_smile:

1 Like

Hey @borisHuezo! That's awesome, so glad you found a workaround since this isn't supported in Retool just yet.