How to add URL query parameters dynamically in JavaScript

1 Answer

0 votes
const myURL = new URL('https://www.website.com/orders/index.html?id=8372&age=43')
 
myURL.searchParams.append('state', 'California')
 
const q = myURL.searchParams;
 
console.log(q);
 
q.forEach((value, key) => {
    console.log(key, value);
});
   

 
    
/*
run:
  
URLSearchParams { 'id' => '8372', 'age' => '43', 'state' => 'California' }
id 8372
age 43
state California
  
*/

 



answered Mar 10, 2020 by avibootz
edited Mar 22, 2020 by avibootz

Related questions

3 answers 258 views
1 answer 179 views
1 answer 122 views
2 answers 164 views
1 answer 167 views
167 views asked Aug 7, 2020 by avibootz
1 answer 210 views
...