0

I have a JavaScript function that replaces a space with a + sign. However, a literal + is passed as the parameter in the url and I need it to be convert to %2B. I tried in my JavaScript function to replace the space into %2B but it ends up converting it back to a plus sign in the URL.

function replacespace() { var p = document.getElementById('keywords') p.value = p.value.replace(/\s+/g, encodeURIComponent('+')); }

Any ideas on how to force the url to use %2B instead?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
loulou
  • 89
  • 2
  • 10
  • 2
    You should add your function to your question. – jmargolisvt Jan 09 '20 at 17:40
  • If you want a space on the server you would have to replace the `+` with `%20` and not `%2B`. You might also want to have a look at [When to encode space to plus (+) or %20?](https://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20) – Andreas Jan 09 '20 at 17:54
  • Here's my function: `function replacespace() { var p = document.getElementById('keywords') p.value = p.value.replace(/\s+/g, encodeURIComponent('+')); }` – loulou Jan 09 '20 at 19:19
  • I'm wanting to replace the space with a plus sign but an encoded value %2B, not a literal '+' in the url – loulou Jan 09 '20 at 19:22
  • Note that we generally like to have code in the question, not a comment. I've edited the code into your question this time. However, please [edit] your question now to provide an example of the HTML and the text you are attempting to use with this function. – Heretic Monkey Jan 09 '20 at 19:49

1 Answers1

0

You can use this

encodeURIComponent('+')
Mickael B.
  • 4,755
  • 4
  • 24
  • 48
  • I tried using this but maybe I'm not using it correctly. When I use it in the url I'm still getting... keywords=BIOC+215 instead of keywords=BIOC%2B215 – loulou Jan 09 '20 at 19:20