Skip to content

Instantly share code, notes, and snippets.

@daothanh
Created December 7, 2017 04:37
Show Gist options
  • Save daothanh/b939241b2b683e0a24afc77184fe571f to your computer and use it in GitHub Desktop.
Save daothanh/b939241b2b683e0a24afc77184fe571f to your computer and use it in GitHub Desktop.
function buildUrl(url, parameters){
var query = window.location.search.substring(1), vars = query.split('&'), qs = "";
if (vars.length) {
for(var j in vars) {
if (vars[j] !== "") {
var value = vars[j].split("=");
if (parameters.hasOwnProperty(value[0]) === false) {
parameters[value[0]] = value[1];
}
}
}
}
for(var key in parameters) {
qs += encodeURIComponent(key) + "=" + encodeURIComponent(parameters[key]) + "&";
}
if (qs.length > 0){
qs = qs.substring(0, qs.length-1); //chop off last "&"
url = url + "?" + qs;
}
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment