I have the following code to close a subtab from the "x" button next to the subtab title:

        <script type="text/javascript">

        var onEnclosingSubTabClose = function (result) {
            confirm('You have unsaved work.  Do you want to exit?');

        <!-- Add a listener to handle the closing of the enclosing sub tab --> 
        sforce.console.getEnclosingTabId(function (result) { 
            if (result.id) {
                onEnclosingSubTabClose, { tabId : result.id });
            } else {
                alert('There was an error closing the tab, please try again.');


It's an event listener that triggers when the user closes the subtab using the "x" button. Everything works perfectly, but the only issue I have is when the confirm dialog box appears, if I click "Ok" the subtab closes but if I click "Cancel", the subtab still closes.

Is there anyway I can just cancel out of the action of closing the tab when I click on "Cancel"?

EDIT: Just realize that the code always run whenever you leave the subtab (from normal cancel link on the VF page or event a submit button - both exiting the subtab). Is there a way to specifically bind the confirm box to just the "x" button next to the subtab title, or is that not possible at all?


  • You didn't actually make any attempt to prevent closing the tab. I think you may need to either return a value or call stopPropagation or preventDefault (on the result object), but I don't have enough experience to know for sure. – sfdcfox Apr 19 '17 at 5:18
  • You could use onTabSave() and setTabUnsavedChanges() functions for your functionality – Santanu Boral Apr 19 '17 at 5:45

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.