1function update_query_parameters(key, val) {
2 uri = window.location.href
3 .replace(RegExp("([?&]"+key+"(?=[=&#]|$)[^#&]*|(?=#|$))"), "&"+key+"="+encodeURIComponent(val))
4 .replace(/^([^?&]+)&/, "$1?");
5 return uri;
6}
7// examples: the following may update ?page=4 in url and redirects to
8window.location.href = update_query_parameters('page', '4);