Home  >  Article  >  Web Front-end  >  Solution to garbled characters in escape encoding and unescape decoding Chinese characters_javascript skills

Solution to garbled characters in escape encoding and unescape decoding Chinese characters_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:42:512434browse

In today’s project, I encountered a situation where Chinese characters need to be encoded using JavaScript’s escape and then decoded using unescape. When testing the code segment, garbled characters appeared.
The specific situation is as follows:
First, use EditPlus to open the test page 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>unescape测试</title>
</head>
<body>
<script>
var teststr=escape("脚本之家");
document.write(teststr);
</script>
</body>
</html>

Page printout:

%uFFFD%u0171%uFFFD%u05AE%uFFFD%uFFFD

You can see that something is wrong at this point. Just from the number of characters corresponding to Chinese characters, it is already wrong!
Then use the following code to test the Chinese characters decoded by unescape:

var relstr=unescape("%uFFFD%u0171%uFFFD%u05AE%uFFFD%uFFFD");
document.write(relstr);

Garbled characters appeared: �ű�֮��

Solution:
Open the test.html file with Dreamweaver and discover the problem!
The original paragraph

var teststr=escape("脚本之家");

became

var teststr=escape("&#369;&#1454;");

It can be seen that it is caused by the initial encoding of the editor!
Change the Chinese characters back in Dreamweaver, re-run test.html, and get the corresponding encoding:

%u811A%u672C%u4E4B%u5BB6


At this time, use unescape to decode:

var relstr=unescape("%u811A%u672C%u4E4B%u5BB6");
document.write(relstr);

We got the original correct Chinese character: Script House!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn