add url parameters javascript code example
Example 1: javascript add update query parameter to url
function update_query_parameters(key, val) {
uri = window.location.href
.replace(RegExp("([?&]"+key+"(?=[=&#]|$)[^#&]*|(?=#|$))"), "&"+key+"="+encodeURIComponent(val))
.replace(/^([^?&]+)&/, "$1?");
return uri;
}
window.location.href = update_query_parameters('page', '4);
Example 2: js add query param
function setParam(uri, key, val) {
return uri
.replace(RegExp("([?&]"+key+"(?=[=&#]|$)[^#&]*|(?=#|$))"), "&"+key+"="+encodeURIComponent(val))
.replace(/^([^?&]+)&/, "$1?");
}