Link to home
Start Free TrialLog in
Avatar of bennyv
bennyv

asked on

When to use encodeURI versus encodeURIComponent ?

Hi,

I have read that for proper Unicode support (internationalisation), you need to use encodeURI / encodeURIComponent and decodeURI / decodeURIComponent instead of escape and unescape.

Can somebody please tell me when to use encodeURI as opposed to encodeURIComponent (and decodeURI as opposed to decodeURIComponent) ?  In which all cases are you supposed to use each of them ?

Thanks,
Benny
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Here is the description of the difference:
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthfEncodeURI.asp
"The encodeURI method does not encode the following characters: ":", "/", ";", and "?".
Use encodeURIComponent to encode these characters."



Avatar of bennyv
bennyv

ASKER

Thanks Zvonko.

That much I had found out in MSDN.
But, I would like know when to use each of them.  For example, will I have to encode the characters ":", "/", ";", and "?"   ?    If you can provide some examples, it will be great !!!

The meaning of the description is this: wether you have or you do not have those four characters in your string, you have to use the appropriate decode method as the oposite method for the encode.
So if you used the encodeURI method for encoding, then you should not use decodeURIComponent for decoding, independent wether you have those four characters in your string or not.
That is all.
And the only difference is that encodeURI does NOT encode those four characters and the encodeURIComponent does encode also those four characters.
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial