encodeURI() 函数通过将特定字符的每个实例替换为一个、两个、三或四转义序列来对统一资源标识符 (URI) 进行编码 (该字符的 UTF-8 编码仅为四转义序列)由两个 “代理” 字符组成)。
语法: encodeURI(URI)
URI
一个完整的URI.

返回值

一个新字符串, 表示提供的字符串编码为统一资源标识符 (URI)。

描述

假定一个URI是完整的URI,那么无需对那些保留的并且在URI中有特殊意思的字符进行编码。
http://username:password@www.example.com:80/path/to/file.php?foo=316&bar=this+has+spaces#anchor
encodeURI 会替换所有的字符,但不包括以下字符,即使它们具有适当的UTF-8转义序列:

类型 包含
保留字符 ; , / ? : @ & = + $
非转义的字符 字母 数字 - _ . ! ~ * ‘ ( )
数字符号 #

encodeURIComponent()函数通过将一个,两个,三个或四个表示字符的UTF-8编码的转义序列替换某些字符的每个实例来编码 URI (对于由两个“代理”字符组成的字符而言,将仅是四个转义序列) 。

语法: encodeURIComponent(str);

参数

str
String. URI 的组成部分。

返回值

原字串作为URI组成部分被被编码后的新字符串。

描述

encodeURIComponent 转义除了如下所示外的所有字符:
不转义的字符: A-Z a-z 0-9 - _ . ! ~ ‘ ( )
encodeURIComponent() 和 *encodeURI
有以下几个不同点:

  1. var set1 = ";,/?:@&=+$"; // 保留字符
  2. var set2 = "-_.!~*'()"; // 不转义字符
  3. var set3 = "#"; // 数字标志
  4. var set4 = "ABC abc 123"; // 字母数字字符和空格
  5. console.log(encodeURI(set1)); // ;,/?:@&=+$
  6. console.log(encodeURI(set2)); // -_.!~*'()
  7. console.log(encodeURI(set3)); // #
  8. console.log(encodeURI(set4)); // ABC%20abc%20123 (the space gets encoded as %20)
  9. console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24
  10. console.log(encodeURIComponent(set2)); // -_.!~*'()
  11. console.log(encodeURIComponent(set3)); // %23
  12. console.log(encodeURIComponent(set4)); // ABC%20abc%20123 (the space gets encoded as %20)

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent