Skip to content

Instantly share code, notes, and snippets.

@joshdavenport
Created April 21, 2012 09:55
Show Gist options
  • Select an option

  • Save joshdavenport/2436282 to your computer and use it in GitHub Desktop.

Select an option

Save joshdavenport/2436282 to your computer and use it in GitHub Desktop.
Reload the current page with specified query string variables
function reloadWithQueryStringVars (queryStringVars) {
var existingQueryVars = location.search ? location.search.substring(1).split("&") : [],
currentUrl = location.search ? location.href.replace(location.search,"") : location.href,
newQueryVars = {},
newUrl = currentUrl + "?";
if(existingQueryVars.length > 0) {
for (var i = 0; i < existingQueryVars.length; i++) {
var pair = existingQueryVars[i].split("=");
newQueryVars[pair[0]] = pair[1];
}
}
if(queryStringVars) {
for (var queryStringVar in queryStringVars) {
newQueryVars[queryStringVar] = queryStringVars[queryStringVar];
}
}
if(newQueryVars) {
for (var newQueryVar in newQueryVars) {
newUrl += newQueryVar + "=" + newQueryVars[newQueryVar] + "&";
}
newUrl = newUrl.substring(0, newUrl.length-1);
window.location.href = newUrl;
} else {
window.location.href = location.href;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment