Link to home
Start Free TrialLog in
Avatar of n3089n
n3089n

asked on

How to remove url parameters

Here is my JavaScript:

current_URL = document.location.href;
top.location.href='http://www.IDPCessna.com/Entities.aspx' + current_URL.substring(current_URL.indexOf('?')) +'&QueryType=Sales&TypeSeller='+ document.forms["_ctl0"].SalesEvents1TypeSeller.value;

How can I completely remove the TypeSeller parameter before adding it a second time in line 2?  This parameter does not always exist before reaching line 2, in which case the code works perfectly.  But if it does exist, it get's added again.
Avatar of novicer
novicer

try this:
var url = "http://www.IDPCessna.com/Entities.aspx?QueryType=Sales&TypeSeller=aaaa";
var newurl = current_URL.replace(/&TypeSeller([\S])+/g,"");
the newurl is now http://www.IDPCessna.com/Entities.aspx?QueryType=Sales 
Hope this may help.
SOLUTION
Avatar of novicer
novicer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of n3089n

ASKER

The answer for me:

current_Query=window.location.search;
current_Query=current_Query.replace(/&TypeSeller([\S])+/g,"");
top.location.href='http://www.IDPCessna.com/Entities.aspx' + current_Query +'&QueryType=Sales&TypeSeller='+ document.forms["_ctl0"].SalesEvents1TypeSeller.value;

The QueryType parameter is not part of the current query.  Nevertheless, excellent insight Zvonko.  Thank you both for the fast and excellent help!
You are welcome.
i am working on a good function to do this in JScript (for ASP) but it works in JavaScript too OFC, i know this topic is old but it came up on google and id like to hear what people think (and help ofc if i can)

function removeParamFromURL(URL,param)
{
	URL = String(URL);
	var regex = new RegExp( "\\?" + param + "=[^&]*&?", "gi");
	URL = URL.replace(regex,'?');
	regex = new RegExp( "\\&" + param + "=[^&]*&?", "gi");
	URL = URL.replace(regex,'&');
	URL = URL.replace(/(\?|&)$/,'');
	regex = null;
	return URL;
}
 
function addParamToURL(URL,param,value)
{
	URL = removeParamFromURL(URL,param);
	URL = URL + '&' + param + '=' + value
	if (!(/\?/.test(URL))) URL = URL.replace(/&/,'?');
	return URL;
}

Open in new window