encodeURIComponent() URL 编码
最后更新:2026-09-09
encodeURIComponent() URL 编码
encodeURIComponent 对 URI 组件进行编码——将特殊字符转为百分号编码。构建查询参数的标准方法。
| 分类 | 全局函数 |
|---|---|
| ES 版本 | ES1 |
📝 语法
encodeURIComponent(字符串);
⚙️ 参数
| str | 要编码的字符串。 |
|---|
返回值:编码后的字符串。
是否修改原值:否(返回新值)
▶ 示例
编辑下方代码,点击运行,在输出面板查看结果:
运行结果:
%E6%90%9C%E7%B4%A2%20JavaScript%20%E6%95%99%E7%A8%8B
https://example.com/search?q=JS%20%E6%95%99%E7%A8%8B
💡 提示 encodeURIComponent 编码所有特殊字符(含 /、?、&)——适合编码参数值。
⚠️ 警告 与 encodeURI 的区别:encodeURI 保留 URL 结构字符(:/?#),用于编码完整 URL。
🌐 浏览器支持
| 浏览器 | |||||
|---|---|---|---|---|---|
| encodeURIComponent() | ✓ v1 | ✓ v12 | ✓ v1 | ✓ v1 | ✓ v1 |
| 所有现代浏览器均支持 | |||||
🏷️ 相关条目
💡 相关案例
更多实战案例即将上线,敬请期待。
📚 相关教程
❓ 常见问题
QencodeURIComponent 和 encodeURI 的区别?
AencodeURIComponent 编码所有特殊字符(含 / ? &);encodeURI 保留 URL 结构字符。
Q解码用什么?
AdecodeURIComponent(encoded)。
Q+ 号会被编码吗?
A不会——+ 在 encodeURIComponent 中不编码。空格变为 %20。