Skip to content
Advertisement

Passing string variable with spaces

In the following code:

JavaScript

‘set’ is a string variable which can have spaces in it. I’m noticing when it has spaces it’s not working correctly. How can I fix this?

EDIT: For clarity, I’d like to keep the spaces intact.

Advertisement

Answer

You have to replace intermediate space (' ') with '%20' using replace(), and eliminate boundary spaces (' ') using trim():

JavaScript
Advertisement