文字的编码和解码

encodeURI和encodeURIComponent

这两个方法可以对url进行编码

encodeURI方法不会对下列字符编码:

ASCII字母、数字、~!@#$&*()=:/,;?+'

encodeURIComponent方法不会对下列字符编码 :

ASCII字母、数字、~!*()'

所以encodeURIComponent比encodeURI编码的范围更大,如:encodeURIComponent会把 http://  编码成  http%3A%2F%2F 而encodeURI却不会

ASCII 码使用指定的7 位或8 位二进制数组合来表示128 或256 种可能的字符,是美国标准。

escape

escape是对字符串(string)进行编码

escape不会对一下字符编码:

ASCII字母、数字、@*/+ 

用途

encodeURI("http://www.cnblogs.com/");

如果只是编码字符串,不设计url那么用escape;
如果需要编码整个URL,然后需要使用这个URL,那么用encodeURI;
如果需要编码URL中的参数的时候,那么encodeURIComponent是最好方法;

对应的解码:unescape,decodeURI,decodeURIComponent

 

原文:https://www.cnblogs.com/season-huang/p/3439277.html

posted @ 2020-04-13 13:17  seeBetter  阅读(485)  评论(0)    收藏  举报