Newer Version Available
getEnclosingTabId() for Lightning Experience
Returns the ID of the enclosing tab.
Arguments
None.
LWC Sample Code
To get the ID of the enclosing tab in LWC, use the EnclosingTabId wire adapter.
This component retrieves the enclosing tab ID and closes the tab.
1import { LightningElement, wire } from 'lwc';
2import { EnclosingTabId, closeTab } from 'lightning/platformWorkspaceApi';
3
4export class CloseEnclosingTabExample extends LightningElement {
5 @wire(EnclosingTabId) enclosingTabId;
6
7 handleClick() {
8 if (this.enclosingTabId) {
9 return;
10 }
11 closeTab(this.enclosingTabId);
12 }
13}Aura Components Sample Code
This component has a button that, when pressed, retrieves the enclosing tab ID.
Component code:
1<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
2 <lightning:workspaceAPI aura:id="workspace" />
3 <lightning:button label="Get Enclosing Tab Id" onclick="{! c.getEnclosingTabId }" />
4</aura:component>Controller code:
1({
2 getEnclosingTabId : function(component, event, helper) {
3 var workspaceAPI = component.find("workspace");
4 workspaceAPI.getEnclosingTabId().then(function(tabId) {
5 console.log(tabId);
6 })
7 .catch(function(error) {
8 console.log(error);
9 });
10 }
11})Response
This method returns a promise that, upon success, resolves
to the tabId of the enclosing tab, if within a tab,
or false if not within a tab.