155
rated 0 times
[
156]
[
1]
/ answers: 1 / hits: 21392
/ 10 Years ago, wed, october 8, 2014, 12:00:00
I am trying to create a function to remove a particular querystring and its value from the url .
For eg:
if i have a url like
var url = www.foo.com/test?name=kevin&gender=Male&id=1234
If i pass name -> it should remove the key and value for name. the url should become
www.foo.com/test?gender=Male&id=1234
i have a Function ReturnRefinedURL(key,url)
and i am doing this in the Function
function ReturnRefinedURL(key,url)
{
var Value = getParameterByName(key); // This returns kevin
var stringToBeRemoved = 'key +'='+ Value+'&'; // string becomes 'name=kevin&'
return url.replace(stringToBeRemoved, '');
}
//Found this in Google:
function getParameterByName(name) {
name = name.replace(/[[]/, \[).replace(/[]]/, \]);
var regex = new RegExp([\?&] + name + =([^&#]*)),
results = regex.exec(location.search);
return results == null ? : decodeURIComponent(results[1].replace(/+/g, ));
}
So when i call the method ReturnRefinedURL('name',window.location.href);
This works!!! But looking for a more elegant and fool proof method.
* This wont work if name parameter is the second one in the query string. (the '&' will still be retained)
More From » jquery