To get Salesforce base URL use the window.location object can be used to get the current page address (URL).
The window.location.origin property returns the origin URL of the current page.
baseURL.html:
<template>
<lightning-card title="Salesforce Base URL in Lighting Web Component">
<div class="slds-text-heading_medium slds-text-align-center">
SFDC Base URL : <p></p><lightning-formatted-url value={sfdcBaseURL} ></lightning-formatted-url></p>
</div>
</lightning-card>
</template>
baseURL.js:
import { LightningElement } from 'lwc';
export default class BaseURL extends LightningElement {
sfdcBaseURL;
renderedCallback() {
this.sfdcBaseURL = window.location.origin;
}
}
baseURL.js-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Output:
The window.location.origin property returns the origin URL of the current page.
baseURL.html:
<template>
<lightning-card title="Salesforce Base URL in Lighting Web Component">
<div class="slds-text-heading_medium slds-text-align-center">
SFDC Base URL : <p></p><lightning-formatted-url value={sfdcBaseURL} ></lightning-formatted-url></p>
</div>
</lightning-card>
</template>
baseURL.js:
import { LightningElement } from 'lwc';
export default class BaseURL extends LightningElement {
sfdcBaseURL;
renderedCallback() {
this.sfdcBaseURL = window.location.origin;
}
}
baseURL.js-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Output: