html document 的字符编码问题: content-type 与 meta charset 的区别? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
54qyc
V2EX    HTML

html document 的字符编码问题: content-type 与 meta charset 的区别?

  •  1
     
  •   54qyc Aug 14, 2023 3278 views
    This topic created in 987 days ago, the information mentioned may be changed or developed.

    已知:content-type 指代 HTML 协议的 payload 的类型与编码, 当 payload 是 html document 时,假设 content-type 中 charset 是 A , 那么浏览器会以 A 来解码这个 html document 吗?当浏览器解析 document 匹配到 meta 标签时,如果 meta 标签里的 charset 是 B ,浏览器接下来来会如何解析这个 document ?会换用 B 去解码接下来的字符吗? meta charset 这种设计的应用场景是哪里呢?

    9 replies    2023-09-04 20:36:37 +08:00
    rrfeng
        1
    rrfeng  
       Aug 14, 2023
    content-type 指代 HTML 协议的 payload 的类型与编码
    -----
    content-type 指代 HTTP 协议的 payload 的类型与编码
    i8k
        2
    i8k  
       Aug 14, 2023
    In HTML5, they are equivalent. Use the shorter one, as it is easier to remember and type. Browser upport is fine since it was designed for backwards compatibility.

    https://stackoverflow.com/questions/4696499/meta-charset-utf-8-vs-meta-http-equiv-content-type
    JinTianYi456
        3
    JinTianYi456  
       Aug 14, 2023
    Do both. The header takes precedence, but if the HTML page is accessed locally, there are no HTTP-headers, so you want to have <meta charset="..."> as a safety mechanism.
    54qyc
        4
    54qyc  
    OP
       Aug 14, 2023
    @i8k 这个问题和我的问题不是一个问题
    54qyc
        5
    54qyc  
    OP
       Aug 14, 2023
    @rrfeng 请看内容,假设 content-type 中 charset 是 A , 那么浏览器会以 A 来解码这个 html document 吗?当浏览器解析 document 匹配到 meta 标签时,如果 meta 标签里的 charset 是 B ,浏览器接下来来会如何解析这个 document ?
    54qyc
        6
    54qyc  
    OP
       Aug 14, 2023
    @JinTianYi456 这个我也搜到过,本地解析的话, 浏览器不得线以某种编码解码 html 文件才能读取到 meta 这个标记吗?读取之后再切换编码吗?
    JinTianYi456
        7
    JinTianYi456  
       Aug 14, 2023
    This attribute declares the document's character encoding. If the attribute is present, its value must be an ASCII case-insensitive match for the string "utf-8", because UTF-8 is the only valid encoding for HTML5 documents. <meta> elements which declare a character encoding must be located entirely within the first 1024 bytes of the document.
    rrfeng
        8
    rrfeng  
       Aug 15, 2023
    我理解这其实是两个不同程序的问题:
    网络层告诉渲染器这个文件是啥
    html 里的 meta 是告诉渲染器这个文件内容是啥

    类似于:
    以太网链路层有校验了,为啥 tcp 还要自己加校验?
    chnwillliu
        9
    chnwillliu  
       Sep 4, 2023 via Android
    Http header 优先级高,其次是 meta http-equiv 然后是 meta charset

    应用场景是如果 http header 没有声明 charset 或者压根就不存在 http header 比如打开本地 html 文件。

    HTML5 还定义了更多的各种 encoding sniff 算法,有可能会存在假定编码先解析。

    https://stackoverflow.com/questions/26030572/what-is-the-difference-between-the-charset-in-http-header-and-html-meta
    About     Help     Advertise     Blog     API     FAQ     Solana     3521 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 57ms UTC 11:25 PVG 19:25 LAX 04:25 JFK 07:25
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86