Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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
3  
possible duplicate of How do I parse a URL query parameters, in Javascript? – maazza Aug 22 '15 at 11:20
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;
}
share|improve this answer
    
the url should be coded or decoded ? – Dev pro Aug 22 '15 at 11:25
    
and how can i get data from the return of this function ? – Dev pro Aug 22 '15 at 11:28
    
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 – Girish Jha Aug 22 '15 at 11:42
    
Can you give example ? – Dev pro Aug 22 '15 at 11:47

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.