url编码函数encodeURI和encodeURIComponent

var url = "http://www.wrox.com/illegal value.html#start";
encodeURIComponent(url)  //"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.html%23start"

encodeURI(url)   //"http://www.wrox.com/illegal%20value.html#start"

记住2种特殊字符,一种,浏览器无法识别的特殊字符,如空格、中文。第二种,属于url的特殊字符,如/、//、# 、&等。
encodeURI方法只把第一种特殊字符转义,而encodeURIComponent方法会把两种特殊字符都转义。如果我们需要把url作为参数传给服务器是要用encodeURIComponent方法的。
 

encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。

decodeURIComponent() 解码

encodeURI 编码

decodeURI 解码

原文地址:https://www.cnblogs.com/shuihanxiao/p/9909467.html