最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - encodeURI doesn't escape `equals` - why? - Stack Overflow

programmeradmin3浏览0评论

I have an URI like that:

/dap/module/hdfs-web/api/v1.0/clusters/Cluster%201%20-%20CDH4?operation=copy&to=/user/hdfs/year=2016/partial.txt&overwrite=true

I use encodeURI function to escape string. I'm wondering why spaces are encoded with %20 while equals characters are not?

I have an URI like that:

http://client.dev/dap/module/hdfs-web/api/v1.0/clusters/Cluster%201%20-%20CDH4?operation=copy&to=/user/hdfs/year=2016/partial.txt&overwrite=true

I use encodeURI function to escape string. I'm wondering why spaces are encoded with %20 while equals characters are not?

Share Improve this question asked Jan 27, 2016 at 16:42 Max KoretskyiMax Koretskyi 106k67 gold badges353 silver badges515 bronze badges 2
  • 2 Because = is a proper URI character? – putvande Commented Jan 27, 2016 at 16:43
  • = has a meaning in URIs - it connects keys to values, the space character does not, it is simply a bit of string to make it easier for us humans to distinguish words. – somethinghere Commented Jan 27, 2016 at 16:44
Add a ment  | 

2 Answers 2

Reset to default 7

encodeURI encodes a full URI, and URIs can contain = characters. For instance, if a user types in a URI, a first step to resolve it would be to call encodeURI on it.

If on the other hand you are the one constructing the URI, and the input just determines one field (for instance a search query, when given E=mc² you want to resolve https://www.google./search?q=E%3Dmc%C2%B2), then you are not encoding a full URI, but a URI ponent. Use encodeURIComponent for that:

> encodeURIComponent('= ')
'%3D%20'

The encodeURI() function is used to encode a URI.

This function encodes special characters, except:, / ? : @ & = + $ # (Use encodeURIComponent() to encode these characters).

Tip: Use the decodeURI() function to decode an encoded URI.

SOURCE: W3Schools

发布评论

评论列表(0)

  1. 暂无评论