How to retrieve URL parameters (GET) from a customer code block

This is not a questions but something that I want to share in case that anybody has a similar need.

One example about how to get the parameters from the URL. For example: https://your_softr_page.com/?name=Juan&phone=123123123

<span id="name">name parameter</span><br>
<span id="phone">phone parameter</span><br>

<script>

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
        }
    }
    return false;
};

var name = getUrlParameter('name');
var phone = getUrlParameter('phone');

console.log ("name:",name);
console.log ("phone:",phone);

document.getElementById("name").innerHTML = name;
document.getElementById("phone").innerHTML = phone;

</script>
1 Like

Thank you so much for taking the time to share this! Can I ask you something? How do you retrieve the result of fetching these parameters within our studio?

Hi @Jjenglert
I am using a Custom Block

And then just open the URL with the parameters name and phone in the URL.
For example:
https://son5419.softr.app/test/?name=Juan&phone=123123123

Result:

1 Like

Oh, how wonderful! Thanks for teaching us something new :wink: