function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jonathan.wieseljonathan.wiesel 

Warning before leave page Lightning Experience

I have a Visuaflorce page that works fine with Lightning Experience. It's a page override of an Edit button in a custom object.

In order to avoid the users to leave the page without saving I implemented something like this in javascript in the page:
 
window.onbeforeunload = function() {

    if (...) {
        return 'Are you sure you want to leave? Your changes will be lost';
    }
}
This works like a charm on Classic when the user tries to close the browser, change tabs to another object, or anything that basically makes the Visualforce page be dismissed.

This however doesn't work in LEX. Is there a workaround for this kind of scenarios to avoid users lose changes for navigating away from the page?
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Jonathan,

At this moment, I'm afraid there is no documented solution for what you're looking for. Even if you manage to attach the unload event in LEX it wouldn't work because when you change the location in LEX you're not unloading the Window.

Hope to have helped!

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Erick MoriErick Mori
Hi Jonathan,

Look at this link https://developer.salesforce.com/docs/component-library/bundle/lightning:unsavedChanges/documentation.

You can do a logic behind to validate if the user does what you want.

Rgds.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.