Percent-encode, decode and take apart URLs.
encodeURIComponent escapes everything that has a meaning in a URL, which is what you want for a single query value. encodeURI leaves / ? & = alone so a whole URL stays usable.
Form submissions encode spaces as +. Treating them as spaces is almost always what people expect when pasting a query string.