Uncategorized

How to get BASE URL in LWC

We actually don’t need BASE URL when we are using relative URL to work on <a/> tag. However we do need BASE URL when working on absolute URL.

Usually when working on a record, we have record ID, but we need to redirect using a <a/> tag on click of the name. and We may some time need to generate absolute URL and append record if to it. In this blog we will see how we can get BASE URL in lightning web component’s Javascript file.


Click here to know more about relative and absolute URL.

HTML File.

<template>
    <div class="slds-box slds-theme_default">
        BASE URL : {a_Record_URL}
    </div>
</template>

JS File.

import { LightningElement } from 'lwc';

export default class Sample_Test_Component extends LightningElement {
    a_Record_URL;

    connectedCallback(){
        this.a_Record_URL = window.location.origin;
    }   
}

Output.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s