admin管理员组文章数量:1403366
I have to encode the string that I receive here and pass it as a URL parameter so I don't believe I can pass either / or a paranthesis ( so considering I have the following string
KEY WEST / Florida(FL)
I am trying the following
encodeURIComponent("KEY WEST / Florida(FL)")
"KEY%20WEST%20%2F%20Florida(FL)"
escape("KEY WEST / Florida(FL)")
"KEY%20WEST%20/%20Florida%28FL%29"
Neither of them are encoding the string which I can decode later in my code as the first one is keeping the () and the second one is keeping the /
How do I do this in one shot and at a later time decode it when needed?
Also it seems like escape() has been deprecated so which way to do the encoding is preferred?
I have to encode the string that I receive here and pass it as a URL parameter so I don't believe I can pass either / or a paranthesis ( so considering I have the following string
KEY WEST / Florida(FL)
I am trying the following
encodeURIComponent("KEY WEST / Florida(FL)")
"KEY%20WEST%20%2F%20Florida(FL)"
escape("KEY WEST / Florida(FL)")
"KEY%20WEST%20/%20Florida%28FL%29"
Neither of them are encoding the string which I can decode later in my code as the first one is keeping the () and the second one is keeping the /
How do I do this in one shot and at a later time decode it when needed?
Also it seems like escape() has been deprecated so which way to do the encoding is preferred?
Share Improve this question asked Nov 10, 2020 at 22:07 p0ttap0tta 1,6618 gold badges31 silver badges56 bronze badges 4-
1
Where are you needing to decode it? The
encodeURIComponent
should put it in the format that backend servers should have pre-existing logic to know how to decode them. – Taplar Commented Nov 10, 2020 at 22:09 -
If
encodeURIComponent
leaves the( )
alone, you can be confident that the characters do not need to be encoded; that's the whole point. – Pointy Commented Nov 10, 2020 at 22:17 - It's not working for my application. The encoded string is not being recognized by my Angular router. If I enter a plain text like just Key West, it works fine though. – p0tta Commented Nov 10, 2020 at 23:14
- Just added a little more detail on how it's breaking my AngularJS router when I try to pass the param using encodeURIComponent – p0tta Commented Nov 12, 2020 at 0:35
1 Answer
Reset to default 3For URL encoding, encodeURI
and encodeURIComponent
functions should be used.
encodeURI
encodes only special characters, while encodeURIComponent
also encodes characters that have a meaning in the URL, so it is used to encode query strings, for example.
Parentheses are (as explained here), however, allowed anywhere in the URL without encoding them, that's why encodeURIComponent
leaves them as-is.
The escape
function can be considered deprecated, and although it officially isn't, it should be avoided.
so which way to do the encoding is preferred?
- For entire URLs,
encodeURI
- For URL parts, e.g. query string of fragment,
encodeURIComponent
Also see When are you supposed to use escape instead of encodeURI
/ encodeURIComponent
?
本文标签: Encoding all the special characters in JavascriptStack Overflow
版权声明:本文标题:Encoding all the special characters in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744413496a2605075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论