最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get array from url with JavaScript - Stack Overflow

programmeradmin4浏览0评论

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[] ??

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[] ??

Share Improve this question asked Aug 22, 2015 at 11:13 Dev proDev pro 6301 gold badge7 silver badges12 bronze badges 1
  • 4 possible duplicate of How do I parse a URL query parameters, in Javascript? – maazza Commented Aug 22, 2015 at 11:20
Add a comment  | 

3 Answers 3

Reset to default 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).

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;
}

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

发布评论

评论列表(0)

  1. 暂无评论