Htlm files are utf-8 encoded, charset= "gbk", why can browsers still parse correctly?

  1. problem description

html file utf-8 encoding, charset= "gbk", browser parsing is normal;
html file gbk encoding, charset= "utf-8", browser parsing garbled; Why

  1. Code
gbk
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf8">
    <title>Document</title>
</head>
<body>
    <span></span>
</body>
</html>

----------

utf-8
<!DOCTYPE html>
<html>
<head>
    <meta charset="gbk">
    <title>Document</title>
    <style>
        
                
    </style>
</head>
<body>
    <span></span>
</body>
</html>
  1. Screenshot

clipboard.png

clipboard.png

what are you asking?

Apr.07,2021

should be set by your browser to always use utf-8 to decode files by default, so there is the phenomenon you mentioned.


I just specially tested it.
if the html file utf-8 does not have bom encoding, charset= "gbk", the browser parsing will still garbled.


I think your utf8 should be encoded in UTF-8 with BOM format.
I also found that UTF-8 is encoded in BOM format, and the browser can parse other codes such as "ISO-8859-1" instead of charset.

Menu