May 20, 2013

f Comment

JavaScript: How Do You Convert Non ASCII URL Encoded Characters Back to Its Original Form?

Amazon In JavaScript how do you convert a non-ascii URL encoded character back to its raw form? For example how do you convert %E5%88%A5 to ?

別 is a UTF8 based character whose URL encoded value, or percent encoded value, is %E5%88%A5.
Read on to see the solution.

Solution
The following JavaScript function would convert %E5%88%A5 to .

decodeURIComponent('%E5%88%A5')
For some reason Chrome (version 26.0.1410.64 m) and Firefox (version 20.0) encodes percent sign, or %, when saving UTF8 strings (e.g. %25E5%2588%25A5) in cookies.

However when you get the cookie's value from Chrome you get the correct URL encoded strings (e.g. %E5%88%A5) again. In this case simply do the following.

decodeURIComponent(decodeURIComponent('%25E5%2588%25A5'))
Internet Explorer (version 9.0) stores the correct URL encoded strings (e.g. %E5%88%A5) in cookies from the get go.

If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael