编码要发送到 Web 服务器的查询字符串时 - 何时使用escape()以及何时使用encodeURI()或encodeURIComponent() : 
使用转义:
escape("% +&=");要么
使用 encodeURI()/ encodeURIComponent()
encodeURI("http://www.google.com?var1=value1&var2=value2");
encodeURIComponent("var1=value1&var2=value2");不要用它! escape()在B.2.1.2节的转义中定义,附件 B的引言文字说: 
... 本附录中指定的所有语言功能和行为都具有一个或多个不良特征,并且在没有遗留用法的情况下将从本规范中删除。 ...
... 编写新的 ECMAScript 代码时,程序员不应该使用或假设这些特性和行为的存在....
行为:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape
特殊字符编码,但以下情况除外:@ * _ + - ./
字符的十六进制形式,其代码单元值为 0xFF 或更小,是一个两位数的转义序列: %xx 。 
对于具有更大代码单元的字符,使用四位数格式%uxxxx 。在查询字符串中不允许这样做(在RFC3986 中定义): 
query       = *( pchar / "/" / "?" )
pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded   = "%" HEXDIG HEXDIG
sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
              / "*" / "+" / "," / ";" / "="如果是直接跟着两个 hexdigits 百分号只被允许,%,其次是u是不允许的。 
如果需要工作 URL,请使用 encodeURI。拨打这个电话:
encodeURI("http://www.example.org/a file with spaces.html")要得到:
http://www.example.org/a%20file%20with%20spaces.html不要调用 encodeURIComponent,因为它会破坏 URL 并返回
http%3A%2F%2Fwww.example.org%2Fa%20file%20with%20spaces.html如果要对 URL 参数的值进行编码,请使用 encodeURIComponent。
var p1 = encodeURIComponent("http://example.org/?a=12&b=55")然后,您可以创建所需的 URL:
var url = "http://example.net/?param1=" + p1 + "¶m2=99";您将获得这个完整的 URL:
 http://example.net/?param1=http%3A%2F%2Fexample.org%2F%Ffa%3D12%26b%3D55¶m2=99 
请注意,encodeURIComponent 不会转义'字符。一个常见的错误是使用它来创建 html 属性,例如href='MyUrl' ,这可能会遭受注入错误。如果要从字符串构造 html,可以使用"而不是'作为属性引号,或者添加额外的编码层( '可以编码为%27)。 
有关此类编码的更多信息,请查看: http : //en.wikipedia.org/wiki/Percent-encoding
 encodeURI()和encodeURIComponent()之间的区别正是由 encodeURIComponent 编码的 11 个字符,而不是 encodeURI 编码的: 

我使用以下代码在 Google Chrome 中使用console.table轻松生成此表:
var arr = [];
for(var i=0;i<256;i++) {
  var char=String.fromCharCode(i);
  if(encodeURI(char)!==encodeURIComponent(char)) {
    arr.push({
      character:char,
      encodeURI:encodeURI(char),
      encodeURIComponent:encodeURIComponent(char)
    });
  }
}
console.table(arr);我发现这篇文章很有启发性: Javascript Madness:Query String Parsing
当我试图解释为什么 decodeURIComponent 没有正确解码 '+' 时,我发现了它。这是一个摘录:
String:                         "A + B"
Expected Query String Encoding: "A+%2B+B"
escape("A + B") =               "A%20+%20B"     Wrong!
encodeURI("A + B") =            "A%20+%20B"     Wrong!
encodeURIComponent("A + B") =   "A%20%2B%20B"   Acceptable, but strange
Encoded String:                 "A+%2B+B"
Expected Decoding:              "A + B"
unescape("A+%2B+B") =           "A+++B"       Wrong!
decodeURI("A+%2B+B") =          "A+++B"       Wrong!
decodeURIComponent("A+%2B+B") = "A+++B"       Wrong!