9

I'm trying to retrieve url parameters through a lightning web component in my Community.

For exemple : wwww.my-community.com/my-page?test=true

How can I, in the new LWC framework, get the parameter "test" and it's value ?

Thanks for your answers ;)

3 Answers 3

12

If the question was if the LWC Framework provide an utility - then no not that i am aware of.

Which means you can use every js snippet you will find somewhere in the web like

    parameters = {};

    connectedCallback() {

        this.parameters = this.getQueryParameters();
        console.log(this.parameters);
    }

    getQueryParameters() {

        var params = {};
        var search = location.search.substring(1);

        if (search) {
            params = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g, '":"') + '"}', (key, value) => {
                return key === "" ? value : decodeURIComponent(value)
            });
        }

        return params;
    }

That will return you an key/value pair of your parameters for further usage in your component

3
  • Thank you ! :) It works :) I used another script in js because LWC do not have a native function
    – FabienHuot
    Commented Jun 21, 2019 at 9:52
  • Don't forget that only paramters with names starting with "c__" will be kept in the location.href
    – Ombrelin
    Commented Jan 10, 2020 at 11:08
  • VF pages could receive GET/POST Request parameters and read by Apex, Just wondering how LWC cater this? Commented Oct 23, 2020 at 1:35
16

Another approach with using URL.searchParams:

The searchParams readonly property of the URL interface returns a URLSearchParams object allowing access to the GET decoded query arguments contained in the URL.

connectedCallback() {
    const param = 'test';
    const paramValue = this.getUrlParamValue(window.location.href, param);
}

getUrlParamValue(url, key) {
    return new URL(url).searchParams.get(key);
}

15

The best way to get params from URL is use CurrentPageRefference.

currentPageReference.state allows you to get params.

    import { LightningElement, wire } from 'lwc';
    import { CurrentPageReference } from 'lightning/navigation';
    
    export default class Test extends LightningElement {

        currentPageReference = null; 
        urlStateParameters = null;
    
        urlId = null;
        urlLanguage = null;
        urlType = null;

        @wire(CurrentPageReference)
        getStateParameters(currentPageReference) {
           if (currentPageReference) {
              this.urlStateParameters = currentPageReference.state;
              this.setParametersBasedOnUrl();
           }
        }

        setParametersBasedOnUrl() {
           this.urlId = this.urlStateParameters.id || null;
           this.urlLanguage = this.urlStateParameters.lang || null;
           this.urlType = this.urlStateParameters.type || '10';
        }
    }

enter image description here enter image description here

More details you can find here https://beyondthecloud.dev/blog/get-url-parameters-in-lwc

2
  • Note that the state property is not available in Experience Cloud sites: developer.salesforce.com/docs/component-library/bundle/….
    – Vincent
    Commented Nov 15, 2021 at 5:13
  • 2
    @Vincent salesforceprofs.com/wp-content/uploads/2020/11/… Please check the image above. The state is available in Experience Cloud sites. I was using this solution with many Experience Clouds projects and there was no problem with it. What is in your link: Note: Experience Builder sites don't support the state property. Experience Builder is part of Experience Cloud and is for set up the pages, not for users, so it's not relevant. Commented Nov 19, 2021 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .