With javascript how can I add a query string parameter to the url if not present or if it present, update the current value? I am using jquery for my client side development
It's so simple with URLSearchParams, supported in all modern browsers (caniuse).
let p = new URLSearchParams();
p.set("foo", "bar");
p.set("name", "Jack & Jill?");
console.log("http://example.com/?" + p.toString());
If you want to modify the existing URL, construct the object like this: new URLSearchParams(window.location.search)
and assign the string to window.location.search
.