If you are developing for a modern browser, Instead of parsing the url parameters yourself- you can use the built in URL
functions to do it for you like this:
const parser = new URL(url || window.location);
parser.searchParams.set(key, value);
window.location = parser.href;