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?
-
2
Because
=
is a proper URI character? – putvande Commented Jan 27, 2016 at 16:43 -
=
has a meaning in URIs - it connectskeys
tovalues
, 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
2 Answers
Reset to default 7encodeURI
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