Skip to main content
added 4 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

JavaScript Update URL With New Parameter Valuewith new parameter value

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

}

JavaScript Update URL With New Parameter Value

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

}

Update URL with new parameter value

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;
    }
}
Fixed a spelling error
Source Link
WhiteWolf3
  • 41
  • 1
  • 1
  • 5

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

}

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

}

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

}

Source Link
WhiteWolf3
  • 41
  • 1
  • 1
  • 5

JavaScript Update URL With New Parameter Value

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

}