分清encodeURIComponent encodeURI 和 escape

encodeURIComponent

encodeURI

escape (废弃)

目的:这三个函数的作用都是让一段字符串在所有电脑(所有国家区域语言)上可读。

escape属性已经 废弃,不需要关心

encodeURI

 

encodeURIComponent

下面这段转载自网络,很好用:

encodeURI方法不会对下列字符编码  ASCII字母、数字、~!@#$&*()=:/,;?+'
encodeURIComponent方法不会对下列字符编码 ASCII字母、数字、~!*()'
所以encodeURIComponent比encodeURI编码的范围更大。
实际例子来说,encodeURIComponent会把 http://  编码成  http%3A%2F%2F 而encodeURI却不会。
 
1、如果只是编码字符串,不和URL有半毛钱关系,那么用escape。
2、如果你需要编码整个URL,然后需要使用这个URL,那么用encodeURI。
3、当你需要编码URL中的参数的时候,那么encodeURIComponent是最好方法。
原文地址:https://www.cnblogs.com/eret9616/p/9947813.html