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 274 views
1 answer 187 views
1 answer 127 views
2 answers 175 views
1 answer 173 views
173 views asked Aug 7, 2020 by avibootz
1 answer 217 views
...