javascript - Get URL Query String Parameters when there are multiple parameters with the same name using Jquery or JS - Stack Ov

I have a URL that is prised of multiple filters that are named and have corresponding search criteria,

I have a URL that is prised 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

I have a URL that is prised 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 edited Apr 8, 2014 at 16:10 BlueBird asked Apr 8, 2014 at 16:00 BlueBirdBlueBird 1,4666 gold badges24 silver badges36 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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*"
    ]
}

Another solution which works in modern browsers:

let params = (new URL(document.location)).searchParams;
let values = params.getAll("param-name");
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;
}

OR, TO GET MULTIPLES VALUES:

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("=");

    const key = item[0].replace(/\[|\]/g, '')
    const value = item[1]

    if(!result[key]) result[key] = [value]
    else result[key].push(value)

  }
  return result;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745300309a4621396.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信