1

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!

Klaus Mikaelson
  • 572
  • 6
  • 17
  • Does this answer your question? [Get array from url with JavaScript](https://stackoverflow.com/questions/32155462/get-array-from-url-with-javascript) – DBS Dec 08 '22 at 10:09
  • No it works if I had `brand[]=val1&brand[]=val2` but I have `brand[0]=val1&brand[2]=val2` – Klaus Mikaelson Dec 08 '22 at 10:26

1 Answers1

0

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.
}

Moussa Bistami
  • 929
  • 5
  • 15