Skip to content

Instantly share code, notes, and snippets.

@hackerxian
Created December 8, 2015 05:36
Show Gist options
  • Save hackerxian/ac7409993ae790288f09 to your computer and use it in GitHub Desktop.
Save hackerxian/ac7409993ae790288f09 to your computer and use it in GitHub Desktop.
update url parameter
function addOrUpdateUrlParam(name, value) {
var href = window.location.href;
var regexPage = /[&\?]page=\d+/;
href = decodeURIComponent(href.replace(regexPage, ""));
var regex = new RegExp("[&\\?]" + name + "=");
if (regex.test(href)) {
//regex = new RegExp("([&\\?])" + name + "=(\\w+(,\\w+)\*)");
regex = new RegExp("([&\\?])" + name + "=((\\w+(,\\w+)\*)|([\\u4e00-\\u9fa5]*\\/?[\\u4e00-\\u9fa5]\*))");
if (regex.test(href)) {
window.location.href = href.replace(regex, "$1" + name + "=" + value);
} else {
window.location.href = href.replace(name + "=", name + "=" + value);
}
} else {
if (href.indexOf("?") > -1) {
window.location.href = href + "&" + name + "=" + value;
} else {
window.location.href = href + "?" + name + "=" + value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment