Show alert when navigating away from a chat if there is unsent text

I just crafted a nice message to send in Chat GPT with code samples and other relevant configuration values etc (which took several minutes to write) and accidentally selected the ‘Back’ option in a right click ‘contextual menu’ and therefore lost my message as the browser went back to the previous page.

I then exclaimed a rude word out loud.

It would be nice to be prompted with a message like:

“Are you sure you want to leave?”

if the user tries to navigate away from the page and there is unsent text in the message box.

I believe this functionality can be achieved by using the JavaScript ‘beforeunload’ event?

Thank You.

1 Like