I needed a way to update a parameter in the URL, or add it if it doesn't exist, but keep any other variables the same value. I built this function to do the task, and while it works, I feel like it's taking longer than it should. Does anyone have any suggestion on what I could change or a faster method?
function changeURLParameter(sVariable, sNewValue)
{
var aURLParams = [];
var aParts;
var aParams = (window.location.search).substring(1, (window.location.search).length).split('&');
for (var i = 0; i < aParams.length; i++)
{
aParts = aParams[i].split('=');
aURLParams[aParts[0]] = aParts[1];
}
if (aURLParams[sVariable] != sNewValue)
{
if (sNewValue.toUpperCase() == "ALL")
aURLParams[sVariable] = null;
else
aURLParams[sVariable] = sNewValue;
var sNewURL = window.location.origin + window.location.pathname;
var bFirst = true;
for (var sKey in aURLParams)
{
if (aURLParams[sKey])
{
if (bFirst)
{
sNewURL += "?" + sKey + "=" + aURLParams[sKey];
bFirst = false;
}
else
sNewURL += "&" + sKey + "=" + aURLParams[sKey];
}
}
return sNewURL;
}
}