|
10 | 10 | // 数据传递常需要编码后传递,接收还需反编译,推荐使用encodeURIComponent |
11 | 11 | var url = "http://www.csxiaoyao.com?username='CS逍遥剑仙'&password='19931128'"; |
12 | 12 |
|
13 | | -// 【escape & unescape】 |
| 13 | +// 【 escape & unescape 】 !!!URL编码勿使用此方法 |
14 | 14 | console.log(escape(url));// 编码 http%3A//www.csxiaoyao.com%3Fusername%3D%27CS%u900D%u9065%u5251%u4ED9%27%26password%3D%2719931128%27 |
15 | 15 | console.log(unescape(escape(url)));// 解码 http://www.csxiaoyao.com?username='CS逍遥剑仙'&password='19931128' |
16 | 16 |
|
17 | | -// 【encodeURIComponent & decodeURIComponent】【推荐】 |
| 17 | +// 【 encodeURIComponent & decodeURIComponent 】【推荐】如果URI中含分隔符如 ? 和 #,应使用encodeURIComponent |
18 | 18 | console.log(encodeURIComponent(url));// 编码 http%3A%2F%2Fwww.csxiaoyao.com%3Fusername%3D'CS%E9%80%8D%E9%81%A5%E5%89%91%E4%BB%99'%26password%3D'19931128' |
19 | 19 | console.log(decodeURIComponent(encodeURIComponent(url)));// 解码 http://www.csxiaoyao.com?username='CS逍遥剑仙'&password='19931128' |
20 | 20 |
|
21 | | -// 【encodeURI & decodeURI】 |
| 21 | +// 【 encodeURI & decodeURI 】如果URI中含分隔符如 ? 和 #,应使用encodeURIComponent |
22 | 22 | console.log(encodeURI(url));// 编码 http://www.csxiaoyao.com?username='CS%E9%80%8D%E9%81%A5%E5%89%91%E4%BB%99'&password='19931128' |
23 | 23 | console.log(decodeURI(encodeURI(url)));// 解码 http://www.csxiaoyao.com?username='CS逍遥剑仙'&password='19931128' |
24 | 24 |
|
25 | 25 | // 【区别】 |
26 | 26 | // 三种方法都不会对 ASCII 字母、数字和规定的特殊 ASCII 标点符号进行编码,其余都替换为十六进制转义序列 |
27 | | -// 【escape & unescape】 |
| 27 | + |
| 28 | +// 【 escape & unescape 】 |
28 | 29 | // escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z |
29 | 30 | // 对字符串全部进行转义编码,ECMAScript v3 反对使用该方法,对URL编码勿使用此方法 |
30 | | -// 【encodeURIComponent & decodeURIComponent】 |
| 31 | + |
| 32 | +// 【 encodeURIComponent & decodeURIComponent 】 |
31 | 33 | // encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z |
32 | 34 | // 传递参数时需使用encodeURIComponent,组合的url才不会被#等特殊字符截断 |
33 | | -// 【encodeURI & decodeURI】 |
| 35 | + |
| 36 | +// 【 encodeURI & decodeURI 】 |
34 | 37 | // encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z |
35 | 38 | // 进行url跳转时可以整体使用encodeURI,如果URI中含分隔符如 ? 和 #,应使用encodeURIComponent |
36 | 39 |
|
|
0 commit comments