Solved

When to use encodeURI versus encodeURIComponent ?

Posted on 2004-09-22
5
766 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 50 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now