网络设计网站,泰安人才网电焊工,网站建设进度计划表,wordpress git 7.5第一种#xff1a;escape 和 unescape
escape()不能直接用于URL编码#xff0c;它的真正作用是返回一个字符的Unicode编码值
它的具体规则是#xff0c;除了ASCII字母、数字、标点符号 * _ - . /以外#xff0c;对其他所有字符进行编码。在u0000到u00ff之间…
第一种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 http://localhost:8080/index.html?timeescape(time)titleescape(tile)
地址栏显示结果http://localhost:8080/index.html?time2022-01-09title63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996Eunescape解码let url http://localhost:8080/index.html?timeunescape(2022-01-09)titleunescape(63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E)
地址栏显示结果http://localhost:8080/index.html?time2022-01-09title63元黑糖颗粒固饮第二种encodeURI 和 decodeURI
encodeURI()是Javascript中真正用来对URL编码的函数。
它用于对URL的组成部分进行个别编码除了常见的符号以外对其他一些在网址中有特殊含义的符号; / ? : $ , #也不进行编码。编码后它输出符号的utf-8形式并且在每个字节前加上%然后用十六进制的转义序列形式为%xx对生成的 1 字节、2 字节或 4 字节的字符进行编码。 它对应的解码函数是decodeURI()
需要注意的是它不对单引号’编码。
let url http://localhost:8080/index.html?time2022-01-09title63元黑糖颗粒固饮encodeURI编码
let encodeURI_url encodeURI(url) http://localhost:8080/index.html?time2022-01-09title63%E5%85%83%E9%BB%91%E7%B3%96%E9%A2%97%E7%B2%92%E5%9B%BA%E9%A5%AEdecodeURI解码
decodeURIencodeURI_url “http://localhost:8080/index.html?time2022-01-09title63元黑糖颗粒固饮”第三种encodeURIComponent 和 decodeURIComponent
与encodeURI()的区别是它用于对整个URL进行编码。“; / ? : $ , #”这些在encodeURI()中不被编码的符号在encodeURIComponent()中统统会被编码。 它对应的解码函数是decodeURIComponent()。
let url http://localhost:8080/index.html?time2022-01-09title63元黑糖颗粒固饮encodeURIComponent 编码
let encodeURIComponent _url encodeURIComponent (url) http%3A%2F%2Flocalhost%3A8080%2Findex.html%3Ftime%3D2022-01-09%26title%3D63%E5%85%83%E9%BB%91%E7%B3%96%E9%A2%97%E7%B2%92%E5%9B%BA%E9%A5%AEdecodeURIComponent解码
decodeURIComponentencodeURIComponent _url “http://localhost:8080/index.html?time2022-01-09title63元黑糖颗粒固饮”