如何使用JavaScript实现URL编码和解码的三种方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1350个文字,预计阅读时间需要6分钟。
目录:
1.escape 和 unescape
2.encodeURI 和 decodeURI
3.encodeURIComponent 和 decodeURIComponent
4.总结
5.方法说明
6.返回值
1. escape 和 unescape
- escape(String): 使用转义序列替换字符串中的特殊字符。- unescape(String): 将 escape 方法编码的字符串转换回普通字符串。2. encodeURI 和 decodeURI- encodeURI(String): 对 URI 进行编码,保留某些字符。- decodeURI(String): 解码 encodeURI 方法编码的 URI。
3. encodeURIComponent 和 decodeURIComponent- encodeURIComponent(String): 对 URI 组件进行编码,包括保留更多字符。- decodeURIComponent(String): 解码 encodeURIComponent 方法编码的字符串。
4. 总结这四种方法都是用来处理字符串编码和解码的,以确保字符串在传输过程中不会引起错误。
5. 方法说明- escape: 转义特定字符,例如 &、、 和 /。- unescape: 还原 escape 方法编码的字符串。- encodeURI: 保留字母、数字、某些符号和冒号、分号、和斜杠等。- decodeURI: 解码 encodeURI 方法编码的字符串。- encodeURIComponent: 对整个 URI 组件进行编码。- decodeURIComponent: 解码 encodeURIComponent 方法编码的字符串。
6. 返回值- 这些方法的返回值都是字符串类型,表示编码或解码后的字符串。
目录
- 第一种:escape 和 unescape
- 第二种:encodeURI 和 decodeURI
- 第三种:encodeURIComponent 和 decodeURIComponent
- 总结
第一种:escape 和 unescape
escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值
它的具体规则是,除了ASCII字母、数字、标点符号"@ * _ + - . /"以外,对其他所有字符进行编码。在u0000到u00ff之间的符号被转成%xx的形式,其余符号被转成%uxxxx的形式。对应的解码函数是unescape()。
还有两个点需要注意:
- 首先,无论网页的原始编码是什么,一旦被Javascript编码,就都变为unicode字符。也就是说,Javascipt函数的输入和输出,默认都是Unicode字符。这一点对下面两个函数也适用。
- 其次,escape()不对 "+" 编码。但是我们知道,网页在提交表单的时候,如果有空格,则会被转化为+字符。服务器处理数据的时候,会把+号处理成空格。所以,使用的时候要小心。
escape()编码: const time = 2022-01-09 const tile = '63元黑糖颗粒固饮' let url = "localhost:8080/index.html?time="+escape(time)+"&title="+escape(tile) 地址栏显示结果: "localhost:8080/index.html?time=2022-01-09&title=63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E"
unescape()解码: let url = "localhost:8080/index.html?time="+unescape(2022-01-09)+"&title="+unescape(63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E) 地址栏显示结果: "localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮"
第二种:encodeURI 和 decodeURI
encodeURI()是Javascript中真正用来对URL编码的函数。
它用于对URL的组成部分进行个别编码,除了常见的符号以外,对其他一些在网址中有特殊含义的符号"; / ? : @ & = + $ , #",也不进行编码。编码后,它输出符号的utf-8形式,并且在每个字节前加上%,,然后用十六进制的转义序列(形式为%xx)对生成的 1字节、2字节或 4字节的字符进行编码。
它对应的解码函数是decodeURI()
需要注意的是,它不对单引号'编码。
let url = "localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮" encodeURI()编码: let encodeURI_url = encodeURI(url) = "localhost:8080/index.html?time=2022-01-09&title=63%E5%85%83%E9%BB%91%E7%B3%96%E9%A2%97%E7%B2%92%E5%9B%BA%E9%A5%AE" decodeURI()解码: decodeURI(encodeURI_url )= “localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮”
第三种:encodeURIComponent 和 decodeURIComponent
与encodeURI()的区别是,它用于对整个URL进行编码。"; / ? : @ & = + $ , #",这些在encodeURI()中不被编码的符号,在encodeURIComponent()中统统会被编码。
它对应的解码函数是decodeURIComponent()。
let url = "localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮" encodeURIComponent ()编码: let encodeURIComponent _url = encodeURIComponent (url) = localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮”
总结
到此这篇关于js对url进行编码解码的三种方式的文章就介绍到这了,更多相关js对url编码解码内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
本文共计1350个文字,预计阅读时间需要6分钟。
目录:
1.escape 和 unescape
2.encodeURI 和 decodeURI
3.encodeURIComponent 和 decodeURIComponent
4.总结
5.方法说明
6.返回值
1. escape 和 unescape
- escape(String): 使用转义序列替换字符串中的特殊字符。- unescape(String): 将 escape 方法编码的字符串转换回普通字符串。2. encodeURI 和 decodeURI- encodeURI(String): 对 URI 进行编码,保留某些字符。- decodeURI(String): 解码 encodeURI 方法编码的 URI。
3. encodeURIComponent 和 decodeURIComponent- encodeURIComponent(String): 对 URI 组件进行编码,包括保留更多字符。- decodeURIComponent(String): 解码 encodeURIComponent 方法编码的字符串。
4. 总结这四种方法都是用来处理字符串编码和解码的,以确保字符串在传输过程中不会引起错误。
5. 方法说明- escape: 转义特定字符,例如 &、、 和 /。- unescape: 还原 escape 方法编码的字符串。- encodeURI: 保留字母、数字、某些符号和冒号、分号、和斜杠等。- decodeURI: 解码 encodeURI 方法编码的字符串。- encodeURIComponent: 对整个 URI 组件进行编码。- decodeURIComponent: 解码 encodeURIComponent 方法编码的字符串。
6. 返回值- 这些方法的返回值都是字符串类型,表示编码或解码后的字符串。
目录
- 第一种:escape 和 unescape
- 第二种:encodeURI 和 decodeURI
- 第三种:encodeURIComponent 和 decodeURIComponent
- 总结
第一种:escape 和 unescape
escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值
它的具体规则是,除了ASCII字母、数字、标点符号"@ * _ + - . /"以外,对其他所有字符进行编码。在u0000到u00ff之间的符号被转成%xx的形式,其余符号被转成%uxxxx的形式。对应的解码函数是unescape()。
还有两个点需要注意:
- 首先,无论网页的原始编码是什么,一旦被Javascript编码,就都变为unicode字符。也就是说,Javascipt函数的输入和输出,默认都是Unicode字符。这一点对下面两个函数也适用。
- 其次,escape()不对 "+" 编码。但是我们知道,网页在提交表单的时候,如果有空格,则会被转化为+字符。服务器处理数据的时候,会把+号处理成空格。所以,使用的时候要小心。
escape()编码: const time = 2022-01-09 const tile = '63元黑糖颗粒固饮' let url = "localhost:8080/index.html?time="+escape(time)+"&title="+escape(tile) 地址栏显示结果: "localhost:8080/index.html?time=2022-01-09&title=63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E"
unescape()解码: let url = "localhost:8080/index.html?time="+unescape(2022-01-09)+"&title="+unescape(63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E) 地址栏显示结果: "localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮"
第二种:encodeURI 和 decodeURI
encodeURI()是Javascript中真正用来对URL编码的函数。
它用于对URL的组成部分进行个别编码,除了常见的符号以外,对其他一些在网址中有特殊含义的符号"; / ? : @ & = + $ , #",也不进行编码。编码后,它输出符号的utf-8形式,并且在每个字节前加上%,,然后用十六进制的转义序列(形式为%xx)对生成的 1字节、2字节或 4字节的字符进行编码。
它对应的解码函数是decodeURI()
需要注意的是,它不对单引号'编码。
let url = "localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮" encodeURI()编码: let encodeURI_url = encodeURI(url) = "localhost:8080/index.html?time=2022-01-09&title=63%E5%85%83%E9%BB%91%E7%B3%96%E9%A2%97%E7%B2%92%E5%9B%BA%E9%A5%AE" decodeURI()解码: decodeURI(encodeURI_url )= “localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮”
第三种:encodeURIComponent 和 decodeURIComponent
与encodeURI()的区别是,它用于对整个URL进行编码。"; / ? : @ & = + $ , #",这些在encodeURI()中不被编码的符号,在encodeURIComponent()中统统会被编码。
它对应的解码函数是decodeURIComponent()。
let url = "localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮" encodeURIComponent ()编码: let encodeURIComponent _url = encodeURIComponent (url) = localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮”
总结
到此这篇关于js对url进行编码解码的三种方式的文章就介绍到这了,更多相关js对url编码解码内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

