Hope someone can help me.
I am trying to add a parameter to url I have created the following function that works partially.
The problem is when in the url there is already some parameter with values that have a space
Example : name=Jose%20Rojas
When executing the function this modifies it and returns me: name=Jose+Rojas, adding the sign '+'.
How can it be done so that the value is kept in an original way. (name=Jose%20Rojas)
https://jsfiddle.net/wkLcb3q0/1/
//"http://misite.com/?name=Jose Rojas&year=2021"
var old_url = 'http://misite.com/?name=Jose%20Rojas&year=2021';
console.log(old_url);
change_add_url_parameters('add','new','test10');
function change_add_url_parameters($action,$param_name,$param_values){
var url = new URL(old_url);
var params = url.searchParams;
if ($action=='add') {
params.set($param_name, $param_values);
}else{
params.delete($param_name);
}
console.log(url);
//Return search: "?name=Jose+Rojas&year=2021&new=test10",
history.replaceState(null, null, "?"+params);
}
Regards