函数 描述
encodeuri() 把字符串编码为 uri
encodeuricomponent() 把字符串编码为 uri 组件
escape() 对字符串进行编码
上面是查询来自w3school的资料。那么三者之间有什么区别呢,请容我测试测试。
复制代码 代码如下:
var str = http://localhost:8080/product/index?id=123&attr=456&area=中国;
console.log(encodeuri(str));
console.log(encodeuricomponent(str));
console.log(escape(str));
打印结果如下:
复制代码 代码如下:
http://localhost:8080/product/index?id=123&attr=456&area=%e4%b8%ad%e5%9b%bd
http%3a%2f%2flocalhost%3a8080%2fproduct%2findex%3fid%3d123%26attr%3d456%26area%3d%e4%b8%ad%e5%9b%bd
http%3a//localhost%3a8080/product/index%3fid%3d123%26attr%3d456%26area%3d%u4e2d%u56fd
可以看出,
encodeuri不会对:/?&等uri中起分割作用的字符进行编码;
encodeuricomponent则会。
观察escape则发现,:?&都被转码了,而/没有,w3school解释是,escape函数会对ascii码中字母、数字及符号( * @ - _ + . / )之外的所有字符进行编码。
另外,我们可以看出escape对汉字“中国”编码后结果与前两者不同。w3school也建议不使用该方法,用前两者代替。
以上所述就是本文的全部内容了,希望对大家学习javascript能够有所帮助。