Two ways to specify the character set of the html page

Two ways to specify the character set of the html page

1. Two ways to specify the character set of the html page

Method 1:

<meta charset="utf-8">

Method 2:

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

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 the character set of the HTML file.

2. Two ways to set the character set used by the page

The first one: the server specifies the character set of the response content;

response.setCharacterEncoding("utf-8")

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

The second type: page specifies the character set.

See 1

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

3. Server forwarding

The page has already set the character set

What's going on and why isn't it working?

Cause Analysis

Complete the page jump request (forwarding) through the server

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

f12 captures the network and obtains the character set of the corresponding page as 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");

Server Redirection

What happens to the page if you redirect without specifying the encoding set of the returned data?

The character set is determined by the page, that is, the character set of the page is determined by the declaration of the character set in the page.

For example: the page specifies the character set: iso-8859-1

The browser will also parse it according to iso-8859-1.

In addition, even if the redirection setting returns the encoding set of the 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 is determined by the character set declared in the HTML page itself!

Note: What we are talking about today is the problem of page character set, which is not the same as the garbled characters that appear during pure data interaction!

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

The above are the details of the two methods of specifying the page character set in HTML. For more information about specifying the page character set in HTML, please pay attention to other related articles on 123WORDPRESS.COM!

<<:  Detailed explanation of CSS background and border tag examples

>>:  What magical uses does CSS filter have

Recommend

Super detailed teaching on how to upgrade the version of MySQL

Table of contents 1. Introduction 2. Back up the ...

MySQL backup and recovery design ideas

background First, let me explain the background. ...

How to install docker using YUM

As shown in the following figure: If the version ...

Mini Program to Implement Calculator Function

This article example shares the specific code of ...

Use of Linux file command

1. Command Introduction The file command is used ...

React implements paging effect

This article shares the specific code for React t...

Is the tag li a block-level element?

Why can it set the height, but unlike elements lik...

Summary of Docker Data Storage

Before reading this article, I hope you have a ba...

A brief discussion on two methods to solve space-evenly compatibility issues

Since its launch in 2009, flex has been supported...

What is the length of a function in js?

Table of contents Preface Why How much is it? Num...

Solution to blank page after Vue packaging

1. Solution to the problem that the page is blank...

Vue project implements graphic verification code

This article example shares the specific code of ...

Briefly understand the MYSQL database optimization stage

introduction Have you ever encountered a situatio...

Sample code for implementing the Olympic rings with pure HTML+CSS

Rendering Code - Take the blue and yellow rings a...