Last active
August 17, 2016 13:35
-
-
Save bobbzorzen/395ee5b0d52ce439884f09184d70206c to your computer and use it in GitHub Desktop.
A simple method to append to current querystring
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
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