I'm trying to extract multiple parameters from a query string, where the parameter has the same name, in my case filter
. I can only seem to access the first instance of the filter using the following approach, and I also need to split the key/value out from the colon separator into an easier format to work with, which I'm not sure how best to approach. Grateful if anyone can point me in the right direction.
myURL = example.com?someparam=test&filter=color:"GOLD"&filter=size:"10";
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = urlParams,
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 : decodeURIComponent(sParameterName[1]);
}
}
};
var filters = getUrlParameter('filter');
console.log(filter);
// Outputs...
color:"GOLD"