encodeURI和encodeURICompoent的区别

encodeURI()encodeURIComponent() 是 JavaScript 中用于编码 URL 的内置函数,核心区别在于编码范围和应用场景


一、核心区别

维度 encodeURI() encodeURIComponent()
设计目的 编码完整 URL 编码 URL 的某一部分(如参数)
保留字符 不编码 :/?#@&=+$,; 等保留字符 编码所有非标准字符(包括保留字符)
适用场景 确保整个 URL 结构不被破坏 确保 URL 的某部分(如参数值)合法
解码函数 decodeURI() decodeURIComponent()

二、编码范围对比

1. encodeURI() 的保留字符

不会对以下字符编码(保留 URL 结构):

javascript 复制代码
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

示例

javascript 复制代码
const url = "https://example.com/path?name=John Doe&age=20";
console.log(encodeURI(url));
// 输出: "https://example.com/path?name=John%20Doe&age=20"
// 仅编码空格(转为 %20),保留 : / ? & =

2. encodeURIComponent() 的编码范围

所有非标准字符编码(包括保留字符):

javascript 复制代码
A-Z a-z 0-9 - _ . ! ~ * ' ( )

示例

javascript 复制代码
const param = "John Doe&age=20";
console.log(encodeURIComponent(param));
// 输出: "John%20Doe%26age%3D20"
// 空格 → %20,& → %26,= → %3D

三、典型场景

1. 使用 encodeURI() 的场景

需要确保 URL 整体有效时(如跳转链接):

javascript 复制代码
const fullUrl = "https://example.com/path with space?q=search term";
const encodedUrl = encodeURI(fullUrl);
// 输出: "https://example.com/path%20with%20space?q=search%20term"
// 保留 ? 和 /,仅编码路径和参数中的空格

2. 使用 encodeURIComponent() 的场景

编码 URL 参数的值(防止破坏参数分隔符):

javascript 复制代码
const params = { name: "John & Doe", city: "New York" };
const query = `name=${encodeURIComponent(params.name)}&city=${encodeURIComponent(params.city)}`;
// 结果: "name=John%20%26%20Doe&city=New%20York"
// & 被编码为 %26,空格为 %20

四、错误用法示例

1. 错误使用 encodeURI() 编码参数

javascript 复制代码
const value = "data&123";
const badUrl = "https://example.com?key=" + encodeURI(value);
// 结果: "https://example.com?key=data&123"
// & 未被编码,导致 URL 解析错误(参数分隔符冲突)

2. 错误使用 encodeURIComponent() 编码完整 URL

javascript 复制代码
const url = "https://example.com/path?q=test";
const encoded = encodeURIComponent(url);
// 输出: "https%3A%2F%2Fexample.com%2Fpath%3Fq%3Dtest"
// : / ? 被编码,导致 URL 失效

五、总结

  • encodeURI()
    适用于保留 URL 结构 的场景(如路径、域名)。
    示例:encodeURI("https://example.com/路径?q=值")
  • encodeURIComponent()
    适用于编码 URL 片段 (如参数值、哈希值)。
    示例:encodeURIComponent("q=search term&sort=asc")

六、编码对照表

字符 encodeURI() encodeURIComponent()
空格 %20 %20
: 保留 %3A
/ 保留 %2F
? 保留 %3F
& 保留 %26
= 保留 %3D
中文 %E4%B8%AD %E4%B8%AD

七、最佳实践

  1. 构造 URL 参数时

    javascript 复制代码
    const baseUrl = "https://example.com/search";
    const query = "coffee & tea";
    const safeUrl = `${baseUrl}?q=${encodeURIComponent(query)}`;
    // 结果: "https://example.com/search?q=coffee%20%26%20tea"
  2. 直接使用 URLSearchParams(现代浏览器):

    javascript 复制代码
    const params = new URLSearchParams({ q: "coffee & tea" });
    const url = `https://example.com/search?${params.toString()}`;
    // 自动编码参数值,无需手动调用 encodeURIComponent
相关推荐
魔云连洲24 分钟前
Vue2和Vue3响应式的基本实现
开发语言·前端·javascript·vue.js
Moment37 分钟前
一份没有项目展示的简历,是怎样在面试里输掉的?开源项目或许是你的救命稻草 😭😭😭
前端·后端·面试
CreatorRay39 分钟前
受控组件和非受控组件的区别
前端·javascript·react.js
2501_906801201 小时前
BY组态-低代码web可视化组件
前端·物联网·低代码·数学建模·前端框架
sma2mmm1 小时前
微前端实现方案对比Qiankun VS npm组件
前端·前端框架·npm
月起星九1 小时前
为什么package.json里的npm和npm -v版本不一致?
前端·npm·node.js
孤客网络科技工作室1 小时前
每天学一个 Linux 命令(7):cd
java·linux·前端
努力的搬砖人.2 小时前
Vue 2 和 Vue 3 有什么区别
前端·vue.js·经验分享·面试
Json_181790144802 小时前
python采集淘宝拍立淘按图搜索API接口,json数据示例参考
服务器·前端·数据库
珹洺2 小时前
Java-servlet(十)使用过滤器,请求调度程序和Servlet线程(附带图谱表格更好对比理解)
java·开发语言·前端·hive·hadoop·servlet·html