2

In my URL I have several parameters I want to use. For One parameter I have several values.

Now I'm just creating it all manual, but there will be more params to be taken over. How can I make this future proof?

URL example:

  https://www.domain.be/?address%5B0%5D=Gent&tax%5Bprofiel%5D%5B0%5D=387&tax%5Bprofiel%5D%5B1%5D=388&tax%5Bprofiel%5D%5B2%5D=389&distance=40&units=metric&paged=1&per_page=9&lat=51.054342&lng=3.717424&form=1&action=fs

How I take the value from the URL ( not my code )

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(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 : sParameterName[1];
        }
    }
};

  var vacature = getUrlParameter('tax[profiel][0]');
  var vacature2 = getUrlParameter('tax[profiel][1]');
    if (vacature != null) {
    
       if (vacature == '367'){ vacature = "HVAC";}
       else if (vacature == '711'){ vacature = "Handlanger";}
    }
    
    if (vacature2 != null) {
        if (vacature2 == '367'){ vacature2 = "HVAC";}
        else if (vacature2 == '711'){ vacature2 = "Handlanger";}
    }
 var type = vacature + " " + vacature2;
      document.getElementById('vacature').value = type;

I tried starting with a for loop:

  var myStringArray = getUrlParameter('tax[profiel]');
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
 var vacature[i] = getUrlParameter('tax[profiel][i]');
    console.log(myStringArray[i]);
}

But I guess JS doesn't get the Array and I need to put it into one first? Maybe with the push() method?

  var Array = [];
var arrayLength = --- don't know how to get the amount of vars in url ---;
for (var i = 0; i < arrayLength; i++) {
Array.push(getUrlParameter('tax[profiel][i]');
}

For the next part I think the same arrayLength can be used.

for (var iParam = 0; iParam < arrayLength; iParam++) {
    if (vacature[iParam] != null) {
    
       if (vacature[iParam] == '367'){ vacature[iParam] = "HVAC";}
       else if (vacature[iParam] == '711'){ vacature[iParam] = "Handlanger";}
    }
}

Is this way of thinking worth something? ^_^ Please give me a nudge in the right way!

much obliged!

5
  • 1
    How does your URL query string look like, update the question Commented Jul 15, 2020 at 15:01
  • I feel like you're so close to getting there yourself Commented Jul 15, 2020 at 15:03
  • @KunalMukherjee I Updated it with the URL Commented Jul 15, 2020 at 15:16
  • Try with this - [...new URLSearchParams('address%5B0%5D=Gent&tax%5Bprofiel%5D%5B0%5D=387&tax%5Bprofiel%5D%5B1%5D=388&tax%5Bprofiel%5D%5B2%5D=389&distance=40&units=metric&paged=1&per_page=9&lat=51.054342&lng=3.717424&form=1&action=fs')] Commented Jul 15, 2020 at 15:24
  • Does this answer your question? Get array from url with JavaScript Commented Jul 15, 2020 at 16:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.