GUIDE深度 · Deep dive
encodeURI vs encodeURIComponent
encodeURI assumes a full URL and keeps structural characters such as : / ? # & =; encodeURIComponent assumes one parameter value and encodes them too. The wrong choice breaks URLs or truncates parameters.
Reserved characters
encodeURI keeps : / ? # [ ] @ ! $ & ' ( ) * + , ; =.
encodeURIComponent keeps only A-Z a-z 0-9 - _ . ! ~ * ' ( ).
Choosing
Full URL: encodeURI; a single parameter value: encodeURIComponent.
When building a query string, encode each key and value with encodeURIComponent.
FAQ
Why was :// encoded?
You used encodeURIComponent; use encodeURI for a full URL.
What about spaces in a path?
Encode as %20, not +.
Sources
Related calculators
URL Encoder/DecoderPercent-encode or decode a full URL or a parameter value with UTF-8 support.More guides
URL Encoding BasicsRead guideKEEP READING
继续读
按顺序读,或跳到同主题的其他指南。