In a web application that makes use of AJAX calls, I need to submit a request but add a parameter to the end of the URL, for example:
Original URL:
The simplest solution I can think of is this method, which will return the modified URI. I feel like most of you are working way too hard.
function setParam(uri, key, val) {
return uri
.replace(new RegExp("([?&]"+key+"(?=[=]|$)[^#&]*|(?=#|$))"), "&"+key+"="+encodeURIComponent(val))
.replace(/^([^?&]+)&/, "$1?");
}