最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Japanese Characters Escape and Decoding in JS - Stack Overflow

programmeradmin4浏览0评论

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!

/

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/hcU9C/

Share Improve this question asked Sep 6, 2013 at 10:54 Ritesh Kumar GuptaRitesh Kumar Gupta 5,1917 gold badges50 silver badges71 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

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.

发布评论

评论列表(0)

  1. 暂无评论