Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

When to use encodeURI versus encodeURIComponent ?

Posted on 2004-09-22
5
Medium Priority
?
837 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

721 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