Home > Blockchain >  How to add url params to a static url?
How to add url params to a static url?

Time:07-11

I have this address 'http://www.example.com' I want to add 'fileName=samplefile' url parameter to my static url (http://www.example.com).

const myUrl = new URL('https://example.com);

myUrl.searchParams.append('fileName', 'samplefile');

I tried this approach and that didn't work.

CodePudding user response:

Just change the string in the first line and add the URL parameters as specified in the http querystring definition.

const myUrl = new URL('https.//example.com?fileName=samplefile');
console.log(myUrl.search);

Check out the specification of URL as well, to learn more about that.

CodePudding user response:

You can add/append url params by using URLSearchParams here

let url = new URL('https://example.com');
let params = new URLSearchParams(url.search);

params.append('fileName', 'samplefile');
console.log(params.get('fileName'))

  • Related