0

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

Chaturrin
  • 21
  • 2
  • `+` seems more valid there. See https://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20 or https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20 – vsemozhebuty Aug 23 '21 at 18:04

0 Answers0