JS escape()、encodeURI()和encodeURIComponent()的区别

时间:2021-10-28 05:12:21

1.实例说明:

    var url='http://wx.jnqianle.com/content/images/冰皮月饼.jpg?name=张三丰&age=11';
console.info(window.escape(url));
console.info(window.encodeURI(url));
console.info(window.encodeURIComponent(url));

结果:

http%3A//wx.jnqianle.com/content/images/%u51B0%u76AE%u6708%u997C.jpg%3Fname%3D%u5F20%u4E09%u4E30%26age%3D11
http://wx.jnqianle.com/content/images/%E5%86%B0%E7%9A%AE%E6%9C%88%E9%A5%BC.jpg?name=%E5%BC%A0%E4%B8%89%E4%B8%B0&age=11
http%3A%2F%2Fwx.jnqianle.com%2Fcontent%2Fimages%2F%E5%86%B0%E7%9A%AE%E6%9C%88%E9%A5%BC.jpg%3Fname%3D%E5%BC%A0%E4%B8%89%E4%B8%B0%26age%3D11

注:

1.escape() 用于中文编码,但不能用于整个url字符串编码

2.encodeURI() 专门用户Uri编码中文处理,对于uri中的特殊字符比如冒号、问号、&符号不做处理

3.encodeURIComponent()  则是用于url的组成部分编码,不能用于整个url编码

更多,请参考:http://blog.163.com/m13864039250_1/blog/static/213865248201485112535345/