Understand percent-encoding, why spaces become %20 versus +, reserved vs unreserved characters, and how UTF-8 multi-byte encoding works in URLs.
Frequently Asked Questions
Q1. Why does a space become %20 in some URLs and + in others?
RFC 3986 mandates %20 for spaces across all URL parts. The plus sign (+) for spaces was introduced in the HTML 4 form specification (application/x-www-form-urlencoded) specifically for form queries.
Q2. When should I use encodeURI vs encodeURIComponent in JavaScript?
Use encodeURI() when encoding a complete URL string that already has protocols and paths. Use encodeURIComponent() when encoding query parameter keys or values so that &, =, and ? are safely escaped.
Q3. What happens if a percent sign (%) is not followed by two valid hex digits?
The URL decoder will fail with a URIError: malformed URI sequence. To represent a literal percent sign, encode it as %25.