url字符串编码解码的方法

1.URI的编码方法 
Global对象的encodeURI()和encodeURIComponent()方法可以对url进行字符串编码,以便发送给浏览器。用特殊的utf-8编码替换所有无效的字符。 
a)encodeURI()主要用于整个URL,不会对URL本身的特殊字符比如‘/’、‘:’进行编码。 
b)encodeURIComponent()主要用于URL片段,会对任何字符进行编码。

var url = 'https://yuedu.baidu.com/ebook/';
console.log(encodeURI(url));    //https://yuedu.baidu.com/ebook/
console.log(encodeURIComponent(url));   //https%3A%2F%2Fyuedu.baidu.com%2Febook%2F

一般来说,我们使用encodeURIComponent()方法的时候要比使用encodeURI()更多,因为在实践中更常见的是对查询字符串参数而不是对基础URI进行编码。

2.URI的解码方法 
a)decodeURI(),只能对使用encodeURI()替换的字符进行解码。 
b)decodeURIComponent(),能够解码使用encodeURIComponent()编码的所有字符,即它可以解码任何特殊字符的编码。
--------------------- 
作者:hhh-- 
来源:CSDN 
原文:https://blog.csdn.net/u010117950/article/details/78745445 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/laizhixue/article/details/83858312