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.

2
  • Can you not change the code where the URL is created? Commented Feb 25, 2016 at 10:33
  • I want to remove it dynamically on a button click, for normal queryparams I am able to do but not for these kind of queryparams which are in array formats Commented Feb 25, 2016 at 10:34

5 Answers 5

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));
Sign up to request clarification or add additional context in comments.

Comments

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&', '')

2 Comments

Give me a single solution which would work with both the cases.. @john
@sajalsuraj this solution is working for both firefox and chrome :)
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

Comments

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&?/,'')

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.