1

For example if I have the code:

let c = new URL("http:/google.com");
c.search = "d d=m";
console.log(c.href)

It produces "http://google.com/?d%20d=m"

However, if I do:

let c = new URL("http:/google.com");
c.searchParams.set("d d", "m");
console.log(c.href)

This results in "http://google.com/?d+d=m".

Why do these methods produce two different results?

Alex
  • 131
  • 2
  • 13
  • They use different algorithms to produce the same result. `%20` is the space character URL encoded using "percent encoding". `+` is also the space character, encoded using the "x-www-form-urlencoded" algorithm. – Heretic Monkey Jun 29 '21 at 12:09
  • Does this answer your question? [When to encode space to plus (+) or %20?](https://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20) – Heretic Monkey Jun 29 '21 at 12:09

0 Answers0