简体   繁体   中英

How to get url parameters with same name from URL in js?

For example I have this url https://www.test.com/test.html?categoryid=4&test1=12&test2=65&brand[0]=val1&brand[1]=val2&test3=15

Now how do I get value of brand[0]=val1&brand[1]=val2 but it can be any number of there in the url maybe brand[2],brand[3] etc... or none url can be without this parameter

I need to get if brand parameter is in url and if yes then I need to get all which are availabe in the url

Any help would be great!

So you don't really know if there would be parameters or not so that's why I can propose this solution right here it will parse all your parameters anyways and stack them in the config JSON in case there is no parameters config would be empty then on your DOMloaded event you can handle the cases as you want

const config = {};
const loadConfig = () => {
  const urlQuery = new URLSearchParams(window.location.search);
  urlQuery.forEach((e, k) => {
    config[k] = e;
  });
};
const onLoadEvent = () => {
console.log(config) // should contain all the query string params.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM