URL Encoding Explained
URLs can only contain a limited set of ASCII characters. Any other characters — like spaces, accented letters, or special symbols — must be "percent-encoded" (e.g., a space becomes %20, an equals sign becomes %3D). This ensures the URL can be safely transmitted over the internet.
There are two encoding functions in JavaScript:
- encodeURIComponent: Encodes everything except:
A–Z a–z 0–9 - _ . ! ~ * ' ( ). Use for individual query string values and path segments. - encodeURI: Preserves the URL structure characters (
:/?#[]@!$&'()*+,;=). Use when encoding a complete URL.
FAQ
What is %20 vs + in URLs?
Both represent a space. %20 is the percent-encoded form (used in paths). The plus sign + is used to represent spaces in query strings (form submission format). Most servers accept both, but %20 is more universally correct.