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[]
??
- 4 possible duplicate of How do I parse a URL query parameters, in Javascript? – maazza Commented Aug 22, 2015 at 11:20
3 Answers
Reset to default 18I 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