0

I want to write a method that takes an array of strings and joins them with a + symbol, similarily to what Google does. This is my method:

function getQueryUrl(array) {
  let urlParamsString = array.join("+");
  const url = new URL(window.location);
  url.searchParams.set("query", urlParamsString);
  return url.toString();
}

But instead of getting the cleanly plus-separated URL, the URL API escapes the symbols with %2B. Is there any way to prevent this (apart from straight-up replacing the escaped symbols back to +)?

Selbi
  • 831
  • 6
  • 20
  • Does this answer your question? [URLSearchParams does not return the same string as found in a URL's parameters](https://stackoverflow.com/questions/45516070/urlsearchparams-does-not-return-the-same-string-as-found-in-a-urls-parameters) – Deniz Nov 23 '21 at 22:59
  • 3
    Wouldn't it be better understandable with examples? – Mister Jojo Nov 23 '21 at 23:00

1 Answers1

1

Try unescape() function:

function getQueryUrl(array) {
  let urlParamsString = array.join("+");
  const url = new URL(window.location);
  url.searchParams.set("query", urlParamsString);
  return unescape(url.toString());
}
Mana Shah
  • 379
  • 1
  • 6