I am using lightning:unsavedChanges to achieve this on community.But when I click on browser tab close, it just closes the browser and doesn't display custom demoModal component. I have added below line in component

<lightning:unsavedChanges aura:id="unsaved"
                          ondiscard="{!c.handleDiscard}" />

and in controller. Here demoModal is the name of component which will display modal.

closeEvent: function (component, event, helper){
   window.addEventListener("beforeunload", $A.getCallback(function(event){
       var unsaved = component.find("unsaved");
       unsaved.setUnsavedChanges(true, { label: 'demoModal' });

 handleSave: function(cmp, evt, helper) {

 handleDiscard: function(cmp, evt, helper) {
    // similar to the handleSave method, but for discarding changes

On Inspecting, I found that flow is going inside window.addEventListener but it does not display modal.


lightning:UnsavedChanges works only in the context of console application such as closing the tabs or subtabs and not in browser close/refresh events. So, this cannot be achieved in lightning.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.