<meta charset=“utf-8”> vs <meta http-equiv=“Content-Type”>

前端 未结 9 1062
后悔当初
后悔当初 2020-11-22 01:13

In order to define charset for HTML5 Doctype, which notation should I use?

  1. Short:

     
             
    
    
            
相关标签:
9条回答
  • 2020-11-22 01:57

    While not contesting the other answers, I think the following is worthy of mentioning.

    1. The “long” (http-equiv) notation and the “short” one are equal, whichever comes first wins;
    2. Web server headers will override all the <meta> tags;
    3. BOM (Byte order mark) will override everything, and in many cases it will affect html 4 (and probably other stuff, too);
    4. If you don't declare any encoding, you will probably get your text in “fallback text encoding” that is defined your browser. Neither in Firefox nor in Chrome it's utf-8;
    5. In absence of other clues the browser will attempt to read your document as if it was in ASCII to get the encoding, so you can't use any weird encodings (utf-16 with BOM should do, though);
    6. While the specs say that the encoding declaration must be within the first 512 bytes of the document, most browsers will try reading more than that.

    You can test by running echo 'HTTP/1.1 200 OK\r\nContent-type: text/html; charset=windows-1251\r\n\r\n\xef\xbb\xbf<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta charset="windows-1251"><title>привет</title></head><body>привет</body></html>' | nc -lp 4500 and pointing your browser at localhost:4500. (Of course you will want to change or remove parts. The BOM part is \xef\xbb\xbf. Be wary of the encoding of your shell.)

    Please mind that it's very important that you explicitly declare the encoding. Letting browsers guess can lead to security issues.

    0 讨论(0)
  • 2020-11-22 01:59

    There is some news based on Mozilla Foundation, and sitepoint

    Do not use this value (http-equiv=content-type) as it is obsolete. Prefer the charset attribute on the <meta> element.

    0 讨论(0)
  • 2020-11-22 02:05

    To embed a signature on an email, I would use the long version:

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

    The reason is that not many email readers use html5, so it's always better use old html styles. Actually, it's better to use tables than divs + css as well.

    0 讨论(0)
提交回复
热议问题