Last Updated: December 20, 2017
·
856
· Evgenia Karunus

How to append a query parameter to a URL?

const url = new URL('http://www.example.com?hi=rrr');
url.searchParams.set('hello', 'sss')
return url.href; // => 'http://www.example.com/?hi=rrr&hello=sss'

url.searchParams returns a URLSearchParams object, which is not yet supported on a couple of browsers.
To make it more browser compatible you can install a polyfill:
npm install --save url-search-params-polyfill
import 'url-search-params-polyfill';

Docs: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/set.