I wrote a Lightning Component that has to warn the user if anybody attempts to close it. We also have to cover the scenario of the user clicking the "X" and closing the component/tab manually, not just by the button I put there.

This article seems to indicate that you can do it with a confirm messagebox trapping an event even though elsewhere the tabClosed event is described as :

Indicates that a tab has been closed successfully

Needless to say, the code I wrote does not stop the tab closure regardless of the answer to the confirm. Adding return in front of the confirm also does not work. Does anybody have any ideas?

A stub of the code I wrote is below.


    <lightning:workspaceAPI aura:id="workspace" />

    <!-- event handlers -->
    <aura:handler event="lightning:tabClosed" action="{! c.onTabClosed }"/>

    <button class="slds-button slds-button_brand" type="button" onclick="{! c.handleClose }">


handleClose : function(component, event, helper) {
    //Code that closes the tab
onTabClosed : function(component, event, helper) {
    confirm("Do you really want to close this tab? Your assessment will not be paused");
  • I don't think it's actually possible to block the event. That said, have you tried event.preventDefault and/or event.stopPropagation? One of those might have an effect.
    – sfdcfox
    Nov 13 '17 at 21:21
  • You know, I was so engrossed in the lightning that I forgot basic JS. will try. Nov 13 '17 at 21:38

Your Answer

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

Browse other questions tagged or ask your own question.