1

I am getting problem in removing query string arrays from the URL.

This is my URL -

In Chrome, it displays in the given format -

Var url = "http://mywebsite.com/innovation?agenda%5B%5D=4995&agenda%5B%5D=4993#ideaResult";

Whereas, in mozilla it displays in this format -

 http://mywebsite.com/innovation?agenda[]=4995&agenda[]=4993#ideaResult

I want to remove the particular query paramter.

Suppose I want to remove "4995" then final URL should supposed to be like this-

http://mywebsite.com/innovation?agenda[]=4993#ideaResult

Please help.

sajalsuraj
  • 922
  • 15
  • 29

5 Answers5

2
function removeArrayParam(key, value, sourceURL) {
   var rtn = sourceURL.split("?")[0],
       param,
       params_arr = [],
       queryString = (sourceURL.indexOf("?") !== -1) ? sourceURL.split("?")[1] : "";
   if (queryString !== "") {
       params_arr = queryString.split("&");
       for (var i = params_arr.length - 1; i >= 0; i -= 1) {
           param = params_arr[i].split("[]=")[0];
           paramValue = params_arr[i].split("[]=")[1];
           if (param === key && paramValue === value) {
               params_arr.splice(i, 1);
           }
       }
       if(params_arr.length) {
        rtn = rtn + "?" + params_arr.join("&");
       } 
   }
   return rtn;
}

This function will give you the desired result. Just pass key, value and the hashless url.

var url = window.location.href;
var hash = window.location.hash;
var index_of_hash = url.indexOf(hash) || url.length;
var hashless_url = url.substr(0, index_of_hash);  
var desired_url = removeArrayParam(key, value, unescape(hashless_url));
iit2011081
  • 742
  • 8
  • 26
1

what about replace() function?

var url = " http://mywebsite.com/innovation?agenda[]=4995&agenda[]=4993#ideaResult";

url = url.replace('agenda%5B%5D=4995&', '') 
url = url.replace('agenda[]=4995&', '')
elreeda
  • 4,525
  • 2
  • 18
  • 45
1

You can decode the url to make it consistent

ie.

var url = decodeURI("http://mywebsite.com/innovation?agenda%5B%5D=4995&agenda%5B%5D=4993#ideaResult");

....and then split the string and remove a query string. Theres an example in this question here Remove querystring from URL

Community
  • 1
  • 1
OllieH
  • 262
  • 1
  • 4
1

The decodeURI() function according to Mozilla document:

Replaces each escape sequence in the encoded URI with the character that it represents

So you can use it like below:

decodeURI(window.location).replace(/agenda\[\]=4995&?/,'')
‌‌R‌‌‌.
  • 2,818
  • 26
  • 37
1

Been searching for ages for a non-plugin solution to removing arrays and non-arrays from query strings, and then re-adding. It could maybe be a bit more elegant, but this works very well with all of my test cases.

function removeURLParameter(param, url) {
    url = decodeURI(url).split("?");
    path = url.length == 1 ? "" : url[1];
    path = path.replace(new RegExp("&?"+param+"\\[\\d*\\]=[\\w]+", "g"), "");
    path = path.replace(new RegExp("&?"+param+"=[\\w]+", "g"), "");
    path = path.replace(/^&/, "");
    return url[0] + (path.length
        ? "?" + path
        : "");
}

function addURLParameter(param, val, url) {
    if(typeof val === "object") {
        // recursively add in array structure
        if(val.length) {
            return addURLParameter(
                param + "[]",
                val.splice(-1, 1)[0],
                addURLParameter(param, val, url)
            )
        } else {
            return url;
        }
    } else {
        url = decodeURI(url).split("?");
        path = url.length == 1 ? "" : url[1];
        path += path.length
            ? "&"
            : "";
        path += decodeURI(param + "=" + val);
        return url[0] + "?" + path;
    }
}

How to use it:

url = location.href;
    -> http://example.com/?tags[]=single&tags[]=promo&sold=1

url = removeURLParameter("sold", url)
    -> http://example.com/?tags[]=single&tags[]=promo

url = removeURLParameter("tags", url)
    -> http://example.com/

url = addURLParameter("tags", ["single", "promo"], url)
    -> http://example.com/?tags[]=single&tags[]=promo

url = addURLParameter("sold", 1, url)
    -> http://example.com/?tags[]=single&tags[]=promo&sold=1

Of course, to update a parameter, just remove then add. Feel free to make a dummy function for it.

Keir Simmons
  • 1,634
  • 7
  • 21
  • 37