Home  >  Article  >  Web Front-end  >  Why does Chinese characters appear garbled when JavaScript uses the alert function?

Why does Chinese characters appear garbled when JavaScript uses the alert function?

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-13 16:32:022490browse

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.

Why does Chinese characters appear garbled when JavaScript uses the alert function?

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!

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