Last active
August 29, 2015 13:58
-
-
Save zoxon/10367932 to your computer and use it in GitHub Desktop.
JavaScript: Change the URL in the format of key/value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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