Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

When to use encodeURI versus encodeURIComponent ?

Posted on 2004-09-22
5
Medium Priority
?
850 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:bennyv
  • 3
5 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 12121937
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."



0
 

Author Comment

by:bennyv
ID: 12122029
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 !!!

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12122096
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.
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 150 total points
ID: 12122126
And for your example you have to see the consequences.
If you would encode your URL with encodeURIComponent, then the URL would NOT be readable eny more.
The URL consist of :// and ? characters
But when the query_string search parameter contain those special characters, then you have to use encodeURIComponent.
So you can say: the encodeURIComponent method is for encoding of URL parameters.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question