Skip to content

Instantly share code, notes, and snippets.

@zoxon
Last active August 29, 2015 13:58
Show Gist options
  • Save zoxon/10367932 to your computer and use it in GitHub Desktop.
Save zoxon/10367932 to your computer and use it in GitHub Desktop.
JavaScript: Change the URL in the format of key/value
// Change the URL in the format of key/value
function UpdateQueryString(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([/])" + key + "/.*?(/|$)", "gi");
var separator = '/';
if (url.match(re)) {
return url.replace(re, '$1' + key + "/" + value + '$2');
}
else {
return url + separator + key + "/" + value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment