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.
Ex URL: https://lwcpractice1-dev-ed.lightning.force.com/lightning/r/Account/001B000000vZWOHIA4/view
HTML File
JS File
Output
The window.location.origin property returns the origin URL of the current page.
Ex URL: https://lwcpractice1-dev-ed.lightning.force.com/lightning/r/Account/001B000000vZWOHIA4/view
HTML File
<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>
JS File
import { LightningElement } from 'lwc';
export default class BaseURL extends LightningElement {
sfdcBaseURL;
renderedCallback() {
this.sfdcBaseURL = window.location.origin;
}
}
Output
Thanks for this code, very useful.
ReplyDeletethank you so much
ReplyDeleteHow to get that in new tab ?
ReplyDeleteThis helped, thanks a lot
ReplyDelete