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.
Written by lakesare
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Js
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#