这两种方法中哪一种应该用于编码URL?
这取决于你实际想要做什么。
encodeURI 假设输入是一个完整的 URI,其中可能包含一些需要编码的字符。
encodeURIComponent将对所有具有特殊含义的内容进行编码,因此您可以将其用于 URI 的组件,例如
var world = "A string with symbols & characters that have special meaning?";
var uri = 'http://example.com/foo?hello=' + encodeURIComponent(world);
如果你要编码一个字符串并将其放入URL组件(查询字符串参数)中,应该调用 encodeURIComponent
。
如果你要对现有的URL进行编码,请使用encodeURI
。
xkr.us有一个很好的讨论,并附有示例。引用他们的总结:
escape()方法不会对+字符进行编码,而该字符被解释为空格,在服务器端和表单填写的带有空格字段时生成。 由于这个缺点以及这个函数不能正确处理非ASCII字符,所以应尽可能避免使用escape()。 最好的替代方案通常是encodeURIComponent()。
escape()不会对@*/+进行编码
与escape()相比,使用encodeURI()方法更加专业化,因为它对URI进行编码,而不是查询字符串,后者是URL的一部分。 当您需要编码一个字符串以用于任何需要保留某些字符未编码的URI资源时,请使用此方法。 请注意,此方法不会对 '字符进行编码,因为它是URI中的有效字符。
encodeURI()不会对~!@#$&*()=:/,;?+进行编码
最后,在大多数情况下,应使用encodeURIComponent()方法对URI的单个组件进行编码。 此方法将对在URI中通常被识别为特殊字符的某些字符进行编码,以便包括许多组件。 请注意,此方法不会对 '字符进行编码,因为它是URI中的有效字符。
encodeURIComponent()不会对~!*()'进行编码
encodeURI
会失败。 - gman这里是总结。
escape()
不会对 @ * _ + - . / 进行编码,不要使用它。
encodeURI()
不会对 A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # 进行编码。当你的输入是完整的 URL(例如'https://searchexample.com/search?q=wiki')时,请使用它。
encodeURIComponent()
不会对 A-Z a-z 0-9 - _ . ! ~ * ' ( ) 进行编码。当你的输入是完整 URL 的一部分时,请使用它。例如:const queryStr = encodeURIComponent(someString)
https://example.test/?url=https%3A%2F%2Fexample.test%2fhome
- Paul WatsonencodeURI 和 encodeURIComponent 有不同的用途。
一些不同点如下:
encodeURI 用于编码整个URL,而encodeURIComponent 用于编码URI中的组件,例如查询字符串。
有11个字符不被encodeURI编码,但被encodeURIComponent编码。清单如下:
字符 | encodeURI | encodeURIComponent |
---|---|---|
# | # | %23 |
$ | $ | %24 |
& | & | %26 |
+ | + | %2B |
, | , | %2C |
/ | / | %2F |
: | : | %3A |
; | ; | %3B |
= | = | %3D |
? | ? | %3F |
@ | @ | %40 |
注意:
encodeURIComponent 不会编码-_.!~*'(),如果想要这些字符被编码,必须将它们替换为相应的UTF-8字符序列。
encodeURIComponent() :假设其参数是URI的某一部分(例如协议、主机名、路径或查询字符串)。因此,它会转义用于分隔URI部分的标点符号字符。
encodeURI():用于编码现有URL。
encodeURI
和encodeURIComponent
的区别:encodeURIComponent(value)
主要用于编码查询字符串参数值,并对value
中的每个适用字符进行编码。而encodeURI
则忽略协议前缀(http://
)和域名。
在非常罕见的情况下,如果您想要实现手动编码以编码附加字符(尽管它们在典型情况下不需要被编码),比如:! *
,那么您可能会使用:
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
(来源)
*
是无用的,请随意在您的主目录上运行rm *
。 - HoldOffHunger其他答案已经描述了其目的。以下是每个功能实际转换的字符:
control = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F'
+ '\x10\x11\x12\x13\x14\X15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F'
+ '\x7F'
encodeURI (control + ' "%<>[\\]^`{|}' )
encodeURIComponent(control + ' "%<>[\\]^`{|}' + '#$&,:;=?' + '+/@' )
escape (control + ' "%<>[\\]^`{|}' + '#$&,:;=?' + "!'()~")
以上所有字符都被转换为百分号十六进制代码。空格变成%20
,百分号变成%25
等等。以下字符不会被转换。
以下是函数不会转换的字符:
pass_thru = '*-._0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
encodeURI (pass_thru + '#$&,:;=?' + '+/@' + "!'()~")
encodeURIComponent(pass_thru + "!'()~")
escape (pass_thru + '+/@' )
encodeURIComponent
。不要因为名称长而害怕它的使用,对我来说,这是更常用的方法。同时,不要被诱惑使用encodeURI,因为你测试它并且它似乎可以正确编码,但它可能不是你想要使用的,即使在名字字段中使用“Fred”进行简单测试成功,但当你使用更高级的文本,如添加一个&符号或一个井号时,它将失败。你可以查看其他答案了解原因。
encodeURI
不会对斜杠/
进行编码,因此:encodeURIComponent("ac/dc")
=>ac%2Fdc
而encodeURI("ac/dc")
=>ac/dc
。 - user993683"encodeURIComponent()和encodeURI()通过使用它们的UTF-8编码替换URL保留字符来对URI进行编码....它们之间的区别在于encodeURI不会对queryString或hash值进行编码...URL不允许许多特殊字符,比如空格或斜杠。然而,这些特殊字符是生活的一部分,因此URL编码被发明出来了。"
来源 - user1063287