Home >Web Front-end >Front-end Q&A >Why does Chinese characters appear garbled when JavaScript uses the alert function?
The reasons why javascript uses the alert function to cause garbled Chinese characters: 1. The encoding of javascript is inconsistent with the encoding of html, and the encoding needs to be modified to make it consistent; 2. The encoding format of html web pages does not support displaying Chinese, and it needs to be included in the html code header Specify the encoding format in the meta tag.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.
Possible reason one:
If the javascript is written in a file and called, it may be because the js encoding is inconsistent with our HTML encoding.
Solution:
When introducing javascript code into html, specify its character set as gb2312 to display Chinese. The code is as follows:
<script charset="gb2312" language="javascript" type="text/javascript" src="ntkoocx.js"></script>
Possible reason two:
The encoding format of the HTML web page does not support the display of Chinese.
Solution:
Add an attribute setting to the header of the html code and specify the encoding format of the html code as utf-8 to support Chinese display. The code is as follows:
<html meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Recommended learning: javascript video tutorial
The above is the detailed content of Why does Chinese characters appear garbled when JavaScript uses the alert function?. For more information, please follow other related articles on the PHP Chinese website!