HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Update URL with new parameter value

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
updatenewwithvalueurlparameter

Problem

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;
    }
}

Solution

I'm not sure about ways to improve performance, but I did notice a couple things.

Guaranteed Returns

If the specified key is already set to the specified value, your function does not return anything. Maybe this is the expected behavior, but I could see it being a problem if you used the function to directly update the URL.

window.location.href = changeURLParameter("thriggle","boring");
// If thriggle already == boring, this redirects you to /undefined


One way to fix that would be to simply remove the If statement if (aURLParams[sVariable] != sNewValue){...}.

Without that check, regardless of whether the parameter is already set to the specified value, it'll be overwritten with the desired value and the URL string will be returned.

Original Sin

window.location.origin does not exist in older versions of Internet Explorer (9 and down). If your user base doesn't overlap with those poor souls still using IE9, feel free to ignore this one, but otherwise you could do something like this:

var sOrigin = window.location.origin ? window.location.origin : window.location.protocol + "//" + window.location.host;
sNewUrl = sOrigin + window.location.pathname;


Edit: even better, as you noted in your followup comment, just update window.location.search directly and you won't have to deal with window.location.origin!

Code Snippets

window.location.href = changeURLParameter("thriggle","boring");
// If thriggle already == boring, this redirects you to /undefined
var sOrigin = window.location.origin ? window.location.origin : window.location.protocol + "//" + window.location.host;
sNewUrl = sOrigin + window.location.pathname;

Context

StackExchange Code Review Q#83346, answer score: 3

Revisions (0)

No revisions yet.