Skip to content

Instantly share code, notes, and snippets.

@bobbzorzen
Last active August 17, 2016 13:35
Show Gist options
  • Save bobbzorzen/395ee5b0d52ce439884f09184d70206c to your computer and use it in GitHub Desktop.
Save bobbzorzen/395ee5b0d52ce439884f09184d70206c to your computer and use it in GitHub Desktop.
A simple method to append to current querystring
appendToQueryString = function (param, val) {
var queryString = window.location.search.replace("?", "");
var parameterListRaw = queryString === "" ? [] : queryString.split("&");
var parameterList = {};
var newQueryString = "?";
for (var i = 0; i < parameterListRaw.length; i++) {
var parameter = parameterListRaw[i].split("=");
parameterList[parameter[0]] = parameter[1];
}
for (var param in dict) {
if (dict.hasOwnProperty(param)) {
if (dict[param]) {
parameterList[param] = dict[param];
} else {
delete parameterList[param];
}
}
}
for (var item in parameterList) {
if (parameterList.hasOwnProperty(item)) {
newQueryString += item + "=" + parameterList[item] + "&";
}
}
newQueryString = newQueryString.replace(/[?&]$/, "");
return location.origin + location.pathname + newQueryString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment