对要发送到web服务器的查询字符串进行编码时-何时使用escape(),何时使用encodeURI()或encodeURIComponent():

使用转义符:

escape("% +&=");

OR

使用encodeURI()/encodeURIComponent()

encodeURI("http://www.google.com?var1=value1&var2=value2");

encodeURIComponent("var1=value1&var2=value2");

当前回答

@johann echavarria答案的现代改写:

控制台日志(阵列(256).fill().map((忽略,i)=>String.fromCharCode(i)).过滤器((字符)=>encodeURI(char)!==encodeURIComponent(字符)? {字符:char,encodeURI:encodeURI(char),encodeURIComponent:encodeURIComponent(char)}:错误))

或者,如果可以使用表,请将console.log替换为console.table(以获得更漂亮的输出)。

其他回答

擒纵机构()

不要使用它!escape()的定义见B.2.1.1节escape,附录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    = "!" / "$" / "&" / "'" / "(" / ")"
              / "*" / "+" / "," / ";" / "="

只有在百分号后面直接跟两个十六进制数字时才允许百分号,不允许百分号后面跟u。

encodeURI()

如果需要工作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

请注意,encodeURI与encodeURIComponent一样,不会转义'字符。

encodeURI组件()

如果要对URL参数的值进行编码,请使用encodeURIComponent。

var p1 = encodeURIComponent("http://example.org/?a=12&b=55")

然后您可以创建所需的URL:

var url = "http://example.net/?param1=" + p1 + "&param2=99";

您将获得以下完整URL:

http://example.net/?param1=http%3A%2F%2Fexample.org%2F%Ffa%3D12%26b%3D55&param2=99

请注意,encodeURIComponent不转义“”字符。一个常见的错误是使用它来创建html属性,例如href='MyUrl',这可能会导致注入错误。如果要从字符串构造html,请使用“而不是”作为属性引号,或添加额外的编码层('可以编码为%27)。

有关此类型编码的详细信息,请检查:http://en.wikipedia.org/wiki/Percent-encoding

我有这个功能。。。

var escapeURIparam = function(url) {
    if (encodeURIComponent) url = encodeURIComponent(url);
    else if (encodeURI) url = encodeURI(url);
    else url = escape(url);
    url = url.replace(/\+/g, '%2B'); // Force the replacement of "+"
    return url;
};

只需自己尝试encodeURI()和encodeURIComponent()。。。

console.log(encodeURIComponent('@#$%^&*'));

输入:@#$%^&*。输出:%40%23%24%25%5E%26*。等等,你怎么了?为什么没有转换?如果您尝试执行linux命令“$string”,这肯定会导致问题。TLDR:您实际上需要fixedEncodeURIComponent()和fixedEncode URI()。长话短说。。。

何时使用encodeURI()?从不encodeURI()在括号编码方面未能遵守RFC3986。按照MDN encodeURI()文档中的定义和进一步解释,使用fixedEncodeURI(。。。

函数fixedEncodeURI(str){return encodeURI(str).replace(/%5B/g,'[').replace(/%5D/g,']');}

何时使用encodeURIComponent()?从不encodeURIComponent()在编码方面未能遵守RFC3986:!'()*. 按照MDN encodeURIComponent()文档中的定义和进一步解释,使用fixedEncodeURIComponents()。。。

函数fixedEncodeURIComponent(str){return encodeURIComponent(str).replace(/[!'()*]/g,函数(c){return“%”+c.charCodeAt(0).toString(16);});}

然后,您可以使用fixedEncodeURI()对单个URL片段进行编码,而fixedEncode URIComponent()将对URL片段和连接器进行编码;或者,简单地说,fixedEncodeURI()不会编码+@?=:#;,$&(因为&和+是常见的URL运算符),但fixedEncodeURIComponent()会。

Java与JavaScript与PHP的小比较表。

1. Java URLEncoder.encode (using UTF8 charset)
2. JavaScript encodeURIComponent
3. JavaScript escape
4. PHP urlencode
5. PHP rawurlencode

char   JAVA JavaScript --PHP---
[ ]     +    %20  %20  +    %20
[!]     %21  !    %21  %21  %21
[*]     *    *    *    %2A  %2A
[']     %27  '    %27  %27  %27 
[(]     %28  (    %28  %28  %28
[)]     %29  )    %29  %29  %29
[;]     %3B  %3B  %3B  %3B  %3B
[:]     %3A  %3A  %3A  %3A  %3A
[@]     %40  %40  @    %40  %40
[&]     %26  %26  %26  %26  %26
[=]     %3D  %3D  %3D  %3D  %3D
[+]     %2B  %2B  +    %2B  %2B
[$]     %24  %24  %24  %24  %24
[,]     %2C  %2C  %2C  %2C  %2C
[/]     %2F  %2F  /    %2F  %2F
[?]     %3F  %3F  %3F  %3F  %3F
[#]     %23  %23  %23  %23  %23
[[]     %5B  %5B  %5B  %5B  %5B
[]]     %5D  %5D  %5D  %5D  %5D
----------------------------------------
[~]     %7E  ~    %7E  %7E  ~
[-]     -    -    -    -    -
[_]     _    _    _    _    _
[%]     %25  %25  %25  %25  %25
[\]     %5C  %5C  %5C  %5C  %5C
----------------------------------------
char  -JAVA-  --JavaScript--  -----PHP------
[ä]   %C3%A4  %C3%A4  %E4     %C3%A4  %C3%A4
[ф]   %D1%84  %D1%84  %u0444  %D1%84  %D1%84

为了编码,javascript提供了三个内置函数-

escape()-不编码@*/+此方法在ECMA 3之后被弃用,因此应避免使用。encodeURI()-不编码~@#$&*()=:/,;?+'它假定URI是一个完整的URI,因此不会对URI中具有特殊含义的保留字符进行编码。当意图转换完整的URL而不是URL的某个特殊段时,使用此方法。示例-encodeURI('http://stackoverflow.com');将给出-http://stackoverflow.comencodeURIComponent()-不编码-__!~*'( )此函数通过用表示字符UTF-8编码的一个、两个、三个或四个转义序列替换某些字符的每个实例来编码统一资源标识符(URI)组件。此方法应用于转换URL的组件。例如,需要附加一些用户输入示例-encodeURIComponent('http://stackoverflow.com');将提供-http%3A%2F%2Stackoverflow.com

所有这些编码都在UTF 8中执行,即字符将转换为UTF-8格式。

encodeURIComponent与encodeURI的不同之处在于它编码保留字符和encodeURI中的数字符号#