What is URL encoding?
URLs can only contain a limited set of characters. Anything else, such as spaces, accented letters or characters with special meaning like & and ?, is replaced by % followed by its UTF-8 bytes in hex. A space becomes %20 and é becomes %C3%A9.
Query value vs whole URL
- Query value / path segment (
encodeURIComponent) encodes everything except letters, digits and- _ . ! ~ * ' ( ). Use it for a single parameter value. - Whole URL (
encodeURI) leaves: / ? # & =alone so the URL keeps working. Use it to tidy a full address with spaces in it.
%20 or +?
In HTML form submissions (application/x-www-form-urlencoded) spaces are sent as +. Elsewhere in a URL, use %20. When decoding, tick the + option if the text came from a form or query string.