Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a URL that is comprised of multiple filters that are named and have corresponding search criteria, I cannot change the way these urls are constructed.

I am aware of how to get query string parameters in a scenario where I am just trying to get the value of one parameter, however I can't seem to figure out how to associate them together appropriately. For example here is the url:

/AllItems.aspx?FilterName=FilterA&FilterMultiValue=*FilterASearchValue*&FilterName=FilterB&FilterMultiValue=*FilterBSearchValue*"

I would like to get the values from this url so that I can associate Filter A with the FilterA Search Value and Associate Filter B with the FilterB Search Value

share|improve this question
add comment

2 Answers 2

you can use reduce to do that:

var u='/AllItems.aspx?FilterName=FilterA&FilterMultiValue=*FilterASearchValue*&FilterName=FilterB&FilterMultiValue=*FilterBSearchValue*'

u.split(/[?&]/).reduce(function(a,b,c){
  var p=b.split("="), k=p[0], v=decodeURIComponent(p[1]);
  if(!p[1])return a;
  a[k]=a[k]||[];
  a[k].push(v);
 return a;
}, {})

which returns an array of params instead of a string, allowing same-name params to repeat:

{   
    "FilterName": [
        "FilterA",
        "FilterB"
    ],
    "FilterMultiValue": [
        "*FilterASearchValue*",
        "*FilterBSearchValue*"
    ]
}
share|improve this answer
add comment
function getParamsFromUrl() {
  var query = location.search.substr(1);
  var params = query.split("&");
  var result = {};
  for(var i=0; i<params.length; i++) {
    var item = params[i].split("=");
    result[item[0]] = item[1];
  }
  return result;
}
share|improve this answer
add comment

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.