Modify URL and add parameter in between the URL using javascript

3924 views javascript
1

I have a long list of urls to be modified.

For example the url is:

https://somedomain.com/?pid=24787CL22311&source=linkkit&url=https%3A%2F%2Fwww.demo.com

So in the above url I want to add subid=1233 just before the &source=linkkit

like this:

 https://some.com/?pid=24787CL22311&subid=1233&source=linkkit&url=https%3A%2F%2Fwww.demo.com

I tried with append, set and replace but did not gave the specific result

Any suggestions ?

answered question

Why do you need to add it before? It's a query string so no matter where you add it..

The order of parameters in the url doesn't matter unless your endpoint is parsing it explicitly and requires that order

Well, when the actual url is opened in the browser it tracks some data and redirects the url to www.demo.com @PatrickEvans

@KamleshKatpara that has nothing to do with the order of parameters

1 Answer

12

You can do something like:

let url = 'https://some.com/?pid=24787CL22311&subid=1233&source=linkkit&url=https%3A%2F%2Fwww.demo.com';

url = url.replace('&source=', '&subid=1233&source=')

posted this

Have an answer?

JD

Please login first before posting an answer.