57

Original URL:

http://yourewebsite.php?id=10&color_id=1

Resulting URL:

http://yourewebsite.php?id=10

I got the function adding Param

function insertParam(key, value){
    key = escape(key); value = escape(value);
    var kvp = document.location.search.substr(1).split('&');
    var i=kvp.length; var x; while(i--) 
    {
        x = kvp[i].split('=');

        if (x[0]==key)
        {
            x[1] = value;
            kvp[i] = x.join('=');
            break;
        }
    }
    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

    //this will reload the page, it's likely better to store this until finished
    document.location.search = kvp.join('&'); 
}

but I need to function to remove Param

6
  • 1
    Just a sidenote - you didn' open the curly,brackets after declaring your function. Commented Jun 5, 2013 at 13:25
  • 1
    We need more details: remove based on what? Only the last? Based on the key? Commented Jun 5, 2013 at 13:28
  • If you only want the first parameter (id), you could split("&")[0]; Commented Jun 5, 2013 at 13:29
  • Thanks for the reply guy. I got the code from this question here- stackoverflow.com/questions/486896/…. And I just want to reverse the code to remove the adding parameter url. I not good in javascript. Commented Jun 5, 2013 at 13:39
  • In the same question there is a class which can also remove parameters: stackoverflow.com/a/487103/1266242 Commented Jun 5, 2013 at 13:46

2 Answers 2

120

Try this. Just pass in the param you want to remove from the URL and the original URL value, and the function will strip it out for you.

function removeParam(key, 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];
            if (param === key) {
                params_arr.splice(i, 1);
            }
        }
        if (params_arr.length) rtn = rtn + "?" + params_arr.join("&");
    }
    return rtn;
}

To use it, simply do something like this:

var originalURL = "http://yourewebsite.com?id=10&color_id=1";
var alteredURL = removeParam("color_id", originalURL);

The var alteredURL will be the output you desire.

Hope it helps!

Sign up to request clarification or add additional context in comments.

7 Comments

Wow!Thanks. It really help's me. Thanks a lot.
if it strips all parameters, you would want to not use this line: rtn = rtn + "?" + params_arr.join("&"); you'd want to test if params_arr.length > 0 first.
You may look in the other answer i edited to push your new url directly to the url bar .
nice. Altered it with simple checking in the end for params_arr length, so we don't get "?" if there are no more get params left. Like so: if (params_arr.length > 0) { rtn = rtn + "?" + params_arr.join("&"); }
here negative loop is intentional, for (var i = params_arr.length - 1; i >= 0; i -= 1) { it will remove if parameter is include multiple times.
|
17
function removeParam(parameter)
{
  var url=document.location.href;
  var urlparts= url.split('?');

 if (urlparts.length>=2)
 {
  var urlBase=urlparts.shift(); 
  var queryString=urlparts.join("?"); 

  var prefix = encodeURIComponent(parameter)+'=';
  var pars = queryString.split(/[&;]/g);
  for (var i= pars.length; i-->0;)               
      if (pars[i].lastIndexOf(prefix, 0)!==-1)   
          pars.splice(i, 1);
  url = urlBase+'?'+pars.join('&');
  window.history.pushState('',document.title,url); // added this line to push the new url directly to url bar .

}
return url;
}

This will resolve your problem

4 Comments

Have combined the window.history.pushState with the replacement from stackoverflow.com/a/7171293/2067690 - works well.
I'd use document.location.search instead of just splitting, and it needs something like this added to preserve hash if (document.location.hash) { url = url + document.location.hash; }
I would use replaceState instead of pushState. It's a matter of opinion, but I don't think that removing a parameter from the url should be a new history state.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.