encodeURI vs encodeURIComponent Explained
The URL specification (RFC 3986) mandates that only certain ASCII characters are valid. Non-ASCII characters (like Chinese or accents) and reserved delimiters must be converted to hexadecimal percent-encoding:
- encodeURIComponent (组件编码):Encodes almost all special characters including
/,?,&,=, and#. Essential when passing one URL as a query string parameter inside another URL. - encodeURI (整链编码):Encodes illegal characters like spaces and Chinese while preserving structural URL characters like
http://, slashes, and question marks.