Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:42:51
Original
2484 people have browsed it

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>
Copy after login

Page printout:

%uFFFD%u0171%uFFFD%u05AE%uFFFD%uFFFD
Copy after login

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);
Copy after login

Garbled characters appeared: �ű�֮��

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

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

became

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

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
Copy after login


At this time, use unescape to decode:

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

We got the original correct Chinese character: Script House!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!