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

前端 未结 9 1084
后悔当初
后悔当初 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:49

    Both forms of the meta charset declaration are equivalent and should work the same across browsers. But, there are a few things you need to remember when declaring your web files character-set as UTF-8:

    1. Save your file(s) in UTF-8 encoding without the byte-order mark (BOM).
    2. Declare the encoding in your HTML files using meta charset (like above).
    3. Your web server must serve your files, declaring the UTF-8 encoding in the Content-Type HTTP header.

    Apache servers are configured to serve files in ISO-8859-1 by default, so you need to add the following line to your .htaccess file:

    AddDefaultCharset UTF-8
    

    This will configure Apache to serve your files declaring UTF-8 encoding in the Content-Type response header, but your files must be saved in UTF-8 (without BOM) to begin with.

    Notepad cannot save your files in UTF-8 without the BOM. A free editor that can is Notepad++. On the program menu bar, select "Encoding > Encode in UTF-8 without BOM". You can also open files and re-save them in UTF-8 using "Encoding > Convert to UTF-8 without BOM".

    More on the Byte Order Mark (BOM) at Wikipedia.

提交回复
热议问题