Create URL Encoded String

Hi.
I have a case where I need to set a URL string in an input by creating it dynamically.
I need this URL to be encoded, i.e. for this case, SPACES should get replaced by %20.

txtDataHookURL.setValue('http://localhost:210/api/Comms?name='+varCampaignName.value);

The campaign name variable here can have a value say some random value.
So, the final string I need should look like:
http://localhost:210/api/Comms?name=some%20random%20value

There is a replace formatted for string values, but that would mean I have to configure all possibilities. There can be any other such special character which requires encoding.
Looking to get an output similar to this in JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

Community Page
Last updated: