Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add or update query string parameter
// Add / Update a key-value pair in the URL query parameters
function updateUrlParameter(uri, key, value) {
// remove the hash part before operating on the uri
var i = uri.indexOf('#');
var hash = i === -1 ? '' : uri.substr(i);
uri = i === -1 ? uri : uri.substr(0, i);
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
uri = uri.replace(re, '$1' + key + "=" + value + '$2');
} else {
uri = uri + separator + key + "=" + value;
}
return uri + hash; // finally append the hash as well
}
@insign

This comment has been minimized.

Copy link

insign commented Nov 23, 2014

Great fix.

@amorgner

This comment has been minimized.

Copy link

amorgner commented Mar 31, 2016

Found the script (thanks for publishing it) and added the option to remove a key-value pair:

function updateUrlParameter(uri, key, value) {
  // remove the hash part before operating on the uri
  var i = uri.indexOf('#');
  var hash = i === -1 ? ''  : uri.substr(i);
  uri = i === -1 ? uri : uri.substr(0, i);

  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";

  if (!value) {
    // remove key-value pair if value is empty
    uri = uri.replace(new RegExp("([&]?)" + key + "=.*?(&|$)", "i"), '');
    if (uri.slice(-1) === '?') {
      uri = uri.slice(0, -1);
    }
  } else if (uri.match(re)) {
    uri = uri.replace(re, '$1' + key + "=" + value + '$2');
  } else {
    uri = uri + separator + key + "=" + value;
  }
  return uri + hash;
}

The script doesn't have a license header. Am I allowed to use the script including my modifications?

@amorgner

This comment has been minimized.

Copy link

amorgner commented Apr 5, 2016

My above version had a bug when removing a URL parameter in the middle, e.g. ?a=1&b=2&c=3 => ?a=1&c=3. This should work with the following version:

function updateUrlParameter(uri, key, value) {
  // remove the hash part before operating on the uri
  var i = uri.indexOf('#');
  var hash = i === -1 ? ''  : uri.substr(i);
  uri = i === -1 ? uri : uri.substr(0, i);

  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";

  if (!value) {
    // remove key-value pair if value is empty
    uri = uri.replace(new RegExp("([?&]?)" + key + "=[^&]*", "i"), '');
    if (uri.slice(-1) === '?') {
      uri = uri.slice(0, -1);
    }
    // replace first occurrence of & by ? if no ? is present
    if (uri.indexOf('?') === -1) uri = uri.replace(/&/, '?');
  } else if (uri.match(re)) {
    uri = uri.replace(re, '$1' + key + "=" + value + '$2');
  } else {
    uri = uri + separator + key + "=" + value;
  }
  return uri + hash;
}
@williamtguerra

This comment has been minimized.

Copy link

williamtguerra commented Apr 11, 2016

thanks for the update

@GDmac

This comment has been minimized.

Copy link

GDmac commented Oct 27, 2016

Nice, but if there are already two of the same keys, then it will only find/replace the first found. But server side (PHP) the last found will actually be set in GET vars

@simsketch

This comment has been minimized.

Copy link

simsketch commented Feb 17, 2017

Thanks @amorgner...gets the job done for me!

@mystroken

This comment has been minimized.

Copy link

mystroken commented Sep 23, 2017

Thanks guys... you just saved me hours of work lol

@gitanupam

This comment has been minimized.

Copy link

gitanupam commented Oct 25, 2018

Nice work @niyazpk and @amorgner - quite helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.