zoxon
4/10/2014 - 10:54 AM

JavaScript: Change the URL in the format of key/value

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;
	}
}