Created
December 8, 2015 05:36
-
-
Save hackerxian/ac7409993ae790288f09 to your computer and use it in GitHub Desktop.
update url parameter
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
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