这两种方法中应该使用哪一种来编码url ?
当前回答
encodeURI和encodeURIComponent用于不同的目的。 一些不同之处在于
encodeURI用于编码完整的URL,而encodeURIComponent用于编码URI组件,例如查询字符串。 有11个字符不是由encodeURI编码的,而是由encodeURIComponent编码的。 列表:
Character | encodeURI | encodeURIComponent |
---|---|---|
# | # | %23 |
$ | $ | %24 |
& | & | %26 |
+ | + | %2B |
, | , | %2C |
/ | / | %2F |
: | : | %3A |
; | ; | %3B |
= | = | %3D |
? | ? | %3F |
@ | @ | %40 |
注: encodeURIComponent不编码-_.!~*'()。如果要对这些字符进行编码,则必须将它们替换为相应的UTF-8序列字符
如果您想了解更多关于encodeURI和encodeURIComponent的信息,请查看参考链接。 参考链接
其他回答
As a general rule use encodeURIComponent. Don't be scared of the long name thinking it's more specific in it's use, to me it's the more commonly used method. Also don't be suckered into using encodeURI because you tested it and it appears to be encoding properly, it's probably not what you meant to use and even though your simple test using "Fred" in a first name field worked, you'll find later when you use more advanced text like adding an ampersand or a hashtag it will fail. You can look at the other answers for the reasons why this is.
以下是摘要。
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)
这取决于你真正想做什么。
encodeURI假设输入是一个完整的URI,其中可能包含一些需要编码的字符。
encodeURIComponent将编码具有特殊含义的所有内容,因此您可以将它用于uri的组件,例如
var world = "A string with symbols & characters that have special meaning?";
var uri = 'http://example.com/foo?hello=' + encodeURIComponent(world);
encodeURI和encodeURIComponent用于不同的目的。 一些不同之处在于
encodeURI用于编码完整的URL,而encodeURIComponent用于编码URI组件,例如查询字符串。 有11个字符不是由encodeURI编码的,而是由encodeURIComponent编码的。 列表:
Character | encodeURI | encodeURIComponent |
---|---|---|
# | # | %23 |
$ | $ | %24 |
& | & | %26 |
+ | + | %2B |
, | , | %2C |
/ | / | %2F |
: | : | %3A |
; | ; | %3B |
= | = | %3D |
? | ? | %3F |
@ | @ | %40 |
注: encodeURIComponent不编码-_.!~*'()。如果要对这些字符进行编码,则必须将它们替换为相应的UTF-8序列字符
如果您想了解更多关于encodeURI和encodeURIComponent的信息,请查看参考链接。 参考链接
着他。我们有一个很好的讨论,有例子。引用他们的总结:
The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming and the fact that this function fails to handle non-ASCII characters correctly, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent(). escape() will not encode: @*/+ Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs. encodeURI() will not encode: ~!@#$&*()=:/,;?+' Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs. encodeURIComponent() will not encode: ~!*()'