1

I am encoding the following string in javascript

encodeURI = "?qry=M & L";

This give me an output

qry=m%20&%20l

So the "&" fromM & L is not getting encoded. What should i do?

askedFeb 9, 2019 at 3:25
codeNinja's user avatar
0

6 Answers6

3

Does not encode characters that have special meaning (reserved characters) for a URI. The following example shows all the parts that a URI "scheme" can possibly contain.

Reference

encodeURI will not encode following special charcaters

A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

let uri = "?qry=M & L"console.log(encodeURI(uri))

So you can useencodeURIComponent ,this will encode all the characters except these

A-Z a-z 0-9 - _ . ! ~ * ' ( )

let uri = "?qry=M & L"console.log(encodeURIComponent(uri))

answeredFeb 9, 2019 at 3:29
Code Maniac's user avatar
Sign up to request clarification or add additional context in comments.

Comments

2

use encoreURIComponent instead to encode& as%26 as shown below. But it also encodes other special chars like? and=

let uri = "?qry=M & L"console.log(encodeURIComponent(uri))

answeredFeb 9, 2019 at 3:34
Dhananjai Pai's user avatar

Comments

1

TheencodeURI() is not going to encode& as it will only encode certain set of special characters. to encode& you need to useencodeURIComponent.

encodeURI encodes everything except:

A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

encodeURIComponent encodes everything except:

A-Z a-z 0-9 - _ . ! ~ * ' ( )

console.log(encodeURIComponent("?qry=M & L"));

Note the difference between the two methods when used to encode URLs.

const URL = "https://www.example.com/resource?query=10&id=20&name=hello%"console.log(encodeURI(URL));console.log(encodeURIComponent(URL));

FromMDN:

Note that encodeURI by itself cannot form proper HTTP GET and POST requests, such as for XMLHTTPRequests, because "&", "+", and "=" are not encoded, which are treated as special characters in GET and POST requests. encodeURIComponent, however, does encode these characters

answeredFeb 9, 2019 at 3:34
Amardeep Bhowmick's user avatar

Comments

0

You should be using encodeURIComponent() instead of encodeURI()

Note: Usually encodeURIComponent() is used to encode a string (query string), that would be put to the URL. If you are using an existing URL to be encoded, then you may use encodeURI()

const uri = "?qry=M & L";console.log(encodeURIComponent(uri));

Reference:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

answeredFeb 9, 2019 at 4:15
Narendra Kamath's user avatar

Comments

0

Reference here:url encodeYou have three options:

escape(str) will not encode: * @ - _ + . /encodeURI(uri) will not encode: ~!@#$&*()=:/,;?+'encodeURIComponent(uri) will not encode: ~!*()'
answeredJun 24, 2019 at 10:56
Logo2k's user avatar

Comments

-1

Fromhere

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

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

And, also seethis answer

So, you'll probably have to do something like

var inputWithSplChars = "M & L";encodeURI  = "?qry=" + encodeURIComponent(inputWithSplChars);

Hope this helps! Cheers!

answeredFeb 9, 2019 at 3:54
Bharat Raj Saya's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.