Home > Web Front-end > HTML Tutorial > How to set html character set

How to set html character set

藏色散人
Release: 2023-01-05 16:11:24
Original
5873 people have browsed it

Methods to set the html character set: 1. Through the "<meta charset="utf-8">" method; 2. Through the "" method.

How to set html character set

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.

How to set the html character set

  • Two ways to specify the page character set

Method 1:

<meta charset="utf-8">
Copy after login

Method 2:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Copy after login

The above are two ways to set the character set of the page to UTF-8 (setting the page character set). The purpose is to tell the browser that The character set of the html file.

  • Two ways to set the character set used by the page

First: The server specifies the response content Character set;

response.setCharacterEncoding("utf-8")
Copy after login

Premise: The server forwards or redirects to the specified interface through the server. ​

Second type: page-specified character set.

See 1

Note: The first method has higher priority than the second method.

  • Server forwarding

## The page has set the character set

What’s going on? Why doesn’t it take effect?

Cause analysis

Completing the page jump request (forwarding) through the server

Return When , the character set of the data to be returned is not specified.

F12 captures the network, and the character set of the corresponding page obtained is iso-8859-1, so garbled characters will appear when the browser parses the page.  

Solution

If it is forwarding, you need to specify the character set of the returned data as utf-8.

response.setCharacterEncoding("utf-8");
Copy after login

Server redirection

If the page is redirected and the encoding set of the returned data is not specified, what will happen to the page?

The character set is determined by the page, that is, the character set of the page is determined by the statement about the character set on the page.

For example: if the character set specified on the page is: iso-8859-1

##, then the browser will parse it according to iso-8859-1.

In addition, even if the redirection sets the encoding set of the returned data, it will be invalid because: redirection will cause data loss!

5. Summary

If the page is forwarded by the server, the character set of the page is determined by the forwarding; otherwise, the character set of the redirected interface or the normal interface Determined by the declared character set of the HTML page itself!

Note: Today we are talking about the page character set problem and the garbled characters that appear during pure data interaction are not the same thing!

In addition, if the character set is specified in both the HTTP response header and meta, a conflict will occur, and the character set of the http response will take effect.

[Recommended learning:

css video tutorial

]

The above is the detailed content of How to set html character set. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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