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
This is my preference, and it covers the cases I can think of. Can anyone think of a way to reduce it to a single replace?
function setParam(uri, key, val) { return uri .replace(RegExp("([?&]"+key+"(?=[=]|$)[^#&]*|(?=#|$))"), "&"+key+"="+encodeURIComponent(val)) .replace(/^([^?&]+)&/, "$1?"); }