I was trying to write a JavaScript method that escapes Japanese Characters.
var esc_str=escape("チャイナモバイル•リミテッド");
var dec_str=decodeURIComponent(esc_str);
//%u30C1%u30E3%u30A4%u30CA%u30E2%u30D0%u30A4%u30EB%u2022%u30EA%u30DF%u30C6%u30C3%u30C9 is dec_str as per debugger- console log.
console.log(dec_str);
While decoding, I am getting following error:
Uncaught URIError: URI malformed
How do we escape Japanese Characters, to decode it back properly?
Any help is appreciated!
http://jsfiddle.net/hcU9C/
The page you linked to says
Note: The escape() function should not be used to encode URIs. Use the encodeURI() function instead.
And that seems to work:
encodeURIComponent("チャイナモバイル•リミテッド");
"%E3%83%81%E3%83%A3%E3%82%A4%E3%83%8A%E3%83%A2%E3%83%90%E3%82%A4%E3%83%AB%E2%80%A2%E3%83%AA%E3%83%9F%E3%83%86%E3%83%83%E3%83%89"
decodeURIComponent("%E3%83%81%E3%83%A3%E3%82%A4%E3%83%8A%E3%83%A2%E3%83%90%E3%82%A4%E3%83%AB%E2%80%A2%E3%83%AA%E3%83%9F%E3%83%86%E3%83%83%E3%83%89")
"チャイナモバイル•リミテッド"
You should ideally use encodeURI
or encodeURIComponent
to encode strings and decodeURI
or decodeURIComponent
respectively to decode the string as escape
& unescape
have been deprecated.
Still if you wish to use escape
for encoding then use unescape
function instead of decodeURIComponent
function to decode string.
From MDN page,
The escape and unescape functions do not work properly for non-ASCII characters and have been deprecated. In JavaScript 1.5 and later, use encodeURI, decodeURI, encodeURIComponent, and decodeURIComponent.
The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With