Escape coding and Unescape decoding Chinese characters ' garbled solutions _javascript tips

Source: Internet
Author: User

Today's project encountered the need to use JavaScript escape encoding Chinese characters and then use unescape decoding, the test Code section when there is a garbled situation.
The details are as follows:
First, open the test page with EditPlus test.html and edit the following HTML code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

Page printout:

%ufffd%u0171%ufffd%u05ae%ufffd%ufffd

At this point you can see that the situation is not right, the number of characters corresponding to the character is already wrong!
Use the following code to test the unescape decoded characters:

var relstr=unescape ("%ufffd%u0171%ufffd%u05ae%ufffd%ufffd");
document.write (RELSTR);

There are garbled: Ű֮

Workaround:
With Dreamweaver open test.html file, found the problem!
The original part of the

var teststr=escape ("cloud-dwelling community");

into a

var teststr=escape ("Ű֮");

Visible is caused by the editor's initial encoding!
In Dreamweaver, the Chinese character is changed back, the test.html is rerun, and the corresponding code is obtained:

%u811a%u672c%u4e4b%u5bb6


Then use Unescape to decode:

var relstr=unescape ("%u811a%u672c%u4e4b%u5bb6");
document.write (RELSTR);

Get the original correct Chinese characters: cloud-dwelling community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.