-
escape 和 unescape
escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值
采用unicode字符集对指定的字符串除0-255以外进行编码。所有的空格符、标点符号、特殊字符以及更多有联系非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Zvar url = "http://localhost:8080/pro?a=1&b=张三&c=aaa"; escape(url) => http%3A//localhost%3A8080/pro%3Fa%3D1%26b%3D%u5F20%u4E09%26c%3Daaa
-
encodeURI 和 decodeURI
把URI字符串采用UTF-8编码格式转化成escape各式的字符串。
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURI()用于整个url编码var url = "http://localhost:8080/pro?a=1&b=张三&c=aaa"; encodeURI(url) --> http://localhost:8080/pro?a=1&b=%E5%BC%A0%E4%B8%89&c=aaa
-
encodeURIComponent 和 decodeURIComponent
与encodeURI()的区别是,它用于对URL的组成部分进行个别编码,而不用于对整个URL进行编码
因此,; / ? : @ & = + $ , #这些在encodeURI()中不被编码的符号,在encodeURIComponent()中统统会被编码。至于具体的编码方法,两者是一样。把URI字符串采用UTF-8编码格式转化成escape格式的字符串
encodeURIComponent()用于参数的传递,参数包含特殊字符可能会造成间断encodeURIComponent('http://www.baidu.com?name=zhang@xiao@jie&order=1') 结果:"http%3A%2F%2Fwww.baidu.com%3Fname%3Dzhang%40xiao%40jie%26order%3D1"
参考
http://www.ruanyifeng.com/blog/2010/02/url_encoding.html
https://blog.csdn.net/u010227042/article/details/121602886
https://segmentfault.com/a/1190000010735689










网友评论