URL(Uniform Resource Locator)只能包含 ASCII 字符:
a-z A-Z0-9- _ . ~其它字符(空格、中文、Emoji、特殊符号)必须 编码成 %XX 格式,称为 URL Encoding / Percent Encoding。
空格 -> %20
中文“你” -> %E4%BD%A0
Emoji 😀 -> %F0%9F%98%80
JS 提供 4 个常用函数:
一条规则:URL 参数用 encodeURIComponent,整 URL 用 encodeURI
| 函数 | 功能 | 示例 |
|---|---|---|
encodeURI() | 编码整个 URL,不编码 : / ? & = # 等 | encodeURI("https://example.com/搜索?q=你好") → "https://example.com/搜索?q=你好"(部分未编码) |
encodeURIComponent() | 编码 URL 参数,几乎编码所有非字母数字字符 | encodeURIComponent("你好") → %E4%BD%A0 |
decodeURI() | 解码 encodeURI() 编码的 URL | "https://example.com/%E6%90%9C%E7%B4%A2" → "https://example.com/搜索" |
decodeURIComponent() | 解码 encodeURIComponent() | %E4%BD%A0%E5%A5%BD → "你好" |
在线编码/解码工具:
手动查询字符编码:
你可以使用任何编程语言或查看 ASCII 码表。例如在 JavaScript 控制台:
// 获取字符的十六进制码
"!".charCodeAt(0).toString(16); // 输出 "21"
"你".charCodeAt(0).toString(16); // 输出 "4f60" (这是 Unicode 码点)