js中两种解码和编码的小区别 en(de)codeURI和de(en)codeURIComponent
废话不多说直接上代码,各位看官一看便知
var url = "http://baidu.com/detail/login test/ab";
var enCode = encodeURI(url);
var test = decodeURI(enCode);
var test1 = decodeURIComponent(enCode);
var enCodeComponent = encodeURIComponent(url);
var test2 = decodeURI(enCodeComponent);
var test3 = decodeURIComponent(enCodeComponent);
document.write("encode "+enCode+"</br>");
document.write("test decodeURI "+test+"</br>");
document.write("test1 decodeURIComponent "+test1+"</br>");
document.write("enCodeComponent "+enCodeComponent+"</br>");
document.write("test2 decodeURI "+test2+"</br>");
document.write("test3 decodeURIComponent "+test3);
结果:
encode http://baidu.com/detail/login%20test/ab
test decodeURI http://baidu.com/detail/login test/ab
test1 decodeURIComponent http://baidu.com/detail/login test/ab
enCodeComponent http%3A%2F%2Fbaidu.com%2Fdetail%2Flogin%20test%2Fab
test2 decodeURI http%3A%2F%2Fbaidu.com%2Fdetail%2Flogin test%2Fab
test3 decodeURIComponent http://baidu.com/detail/login test/ab
总结: 一般而言 encodeUrl 只对一些特定的字符(具体的尚未完全知道:中文,空格等)会进行编码,所以 decodeURI也只会对encodeURL编码的进行解码
enCodeComponent 编码的范围更加的广包含了encodeUrl 的编码范围,同理decodeURIComponent 也能对enCodeComponent 能够编码的字符进行解码
如果非要说他们四者的关系,用下面的集合图来表示:
以上是自己的所想,存在错误,各位领会其中意思即可。有其它发现的欢迎留言