0

The following function is designed to replace a variable in the URL.

It works, but I want to make it so that if you pass in a variable that doesn't exist in the URL, it will add it to the URL.

window.setUrlParameter = function(param, value) {
    const regExp = new RegExp(param + "(.+?)(&|$)", "g");
    const newUrl = window.location.href.replace(regExp, param + "=" + value + "$2");
    window.history.pushState("", "", newUrl);
}

There is the function. It takes the param name that you want to replace, and the value you want to set.

As I stated earlier, I want it to be able to not only replace variables, but set them as well.

Thanks!

4 Answers4

0

You have the regex, you can just test the url for the same thing, and add it if it isn't there

window.setUrlParameter = function(param, value) {
    const regex = new RegExp(param + "(.+?)(&|$)", "g");
    const exist = regex.test(window.location.href);

    let addQS = window.location.search.length > 0 : '&' : '?';
    let newUrl;

    if (exist) {
        newUrl = window.location.href.replace(regExp, param + "=" + value + "$2");
    } else {
        newUrl = window.location.href + addQS + param + "=" + value;
    }

    window.history.pushState("", "", newUrl);
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

This function should also be faster than using a regex, modified from this answer.

(The arrow function is just ES6 syntax, you can use normal functions if you want too)

window.setUrlParameter = (param, value) => {
    var url = window.location.href;
    var hash = location.hash;
    url = url.replace(hash, '');
    if (url.indexOf(param + "=") >= 0)
    {
        var prefix = url.substring(0, url.indexOf(param));
        var suffix = url.substring(url.indexOf(param));
        suffix = suffix.substring(suffix.indexOf("=") + 1);
        suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
        url = prefix + param + "=" + value + suffix;
    }
    else
    {
        if (url.indexOf("?") < 0)
            url += "?" + param + "=" + value;
        else
            url += "&" + param + "=" + value;
    }
    window.history.pushState(null, null, url + hash);
}
YakovL
  • 7,557
  • 12
  • 62
  • 102
Poootaatoooo
  • 316
  • 1
  • 10
0

I'd highly recommend cleaning this up with RegEx, but you could just check whether or not the href includes the parameter

window.setUrlParameter = function(param, value) {
    const regExp = new RegExp(param + "(.+?)(&|$)", "g");
    let newUrl = window.location.href.replace(regExp, param + "=" + value + "$2");
    if (! window.location.href.includes(param) )
        newUrl+='&'+param+'='+value
    window.history.pushState("", "", newUrl);
}
Michael Ward
  • 496
  • 5
  • 13
0

Nobody here used the URLSearchParams API, so I thought I'd show you that way:

function modifyLocation( param, value )
{
  var qParams = window.location.search.split('?')[0];
  var urlParams = new URLSearchParams(qParams);
  if( urlParams.has(param) ){
    urlParams.delete(param);
  }
  urlParams.append(param, value);
  return window.location.href.split('?')[0] + '?' + urlParams.toString();
}

// To test on CodePen, I used editors = 4, because CodePen has that param
var newUrl = modifyLocation('editors','4');
console.log(newUrl);
// window.history.pushState("", "", newUrl);
Brian Gottier
  • 4,522
  • 3
  • 21
  • 37