Solved

When to use encodeURI versus encodeURIComponent ?

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

Independent Software Vendors: 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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

713 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