3

I have Url with parameters i can get all parametrs except arrays this is my url decoded:

name=myname&type=Restaurant Cuisine Moderne / Créative&heure=06 :00 - 06 :30&nbrPers=10&service[]=Canbeprivatized&service[]=Englishspoken&service[]=Françaisparle&option[]=Check&option[]=CreditCard&typeProfile=Amateur

And this is my JavaScript function:

function getUrlParameter(sParam) {
    var sPageURL =decodeURIComponent(window.location.search.substring(1));
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
}

alerts for test:

var name = getUrlParameter('name');
    alert("name ;"+name);//  show : myname
var service= getUrlParameter('service[]');
        alert("servoce:"+service);//  show :only  "Canbeprivatized" i cant get the others service[]

How can i get all service[] and option[] ??

1

3 Answers 3

18

I know that is is a bit late, but very simple solution that worked for me using the Javascript URL object:

url_string = "https://example.com?options[]=one&options[]=two";
url = new URL(url_string);
options = url.searchParams.getAll("options[]");
console.log(options);

If you wanted to return normal parameters which are not part of an array simply change getAll(parameter) to get(parameter).

Sign up to request clarification or add additional context in comments.

Comments

2
function URLToArray(url) {
    var request = {};
    var arr = [];
    var pairs = url.substring(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < pairs.length; i++) {
      var pair = pairs[i].split('=');

      //check we have an array here - add array numeric indexes so the key elem[] is not identical.
      if(endsWith(decodeURIComponent(pair[0]), '[]') ) {
          var arrName = decodeURIComponent(pair[0]).substring(0, decodeURIComponent(pair[0]).length - 2);
          if(!(arrName in arr)) {
              arr.push(arrName);
              arr[arrName] = [];
          }

          arr[arrName].push(decodeURIComponent(pair[1]));
          request[arrName] = arr[arrName];
      } else {
        request[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
      }
    }
    return request;
}
function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

3 Comments

and how can i get data from the return of this function ?
var arrObj = function(yourUrl); iterate over object and use url key to get the values service and option will be an array property in the object
Can you give example ?
2

I found the solution this is the function changements:

function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1));
    var array =[]
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            array.push(sParameterName[1]);
        }
    }
    return array;
}

Now i return an array not one element

Comments

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.