決定 HTTP 標頭的字符集。我應該簡單地把 utf-8 和 fuggedaboutit 放在一起嗎? (Deciding charset for HTTP Headers. Should i simply put utf-8 and fuggedaboutit?)


問題描述

決定 HTTP 標頭的字符集。我應該簡單地把 utf-8 和 fuggedaboutit 放在一起嗎? (Deciding charset for HTTP Headers. Should i simply put utf-8 and fuggedaboutit?)

I was analyzing a page using Google Page Speed http://pagespeed.googlelabs.com/#url=http_3A_2F_2Fqweop.com&mobile=false&rule=SpecifyCharsetEarly

and it says that we should specify an explicit character set in HTTP Headers.

So basically my question is what determines what character set I should be using?

which character sets will have the least size / fastest ?

OR

What kind of savings can I have by using ASCII instead of say UTF-16 ?

Should i simply put utf-8 and fuggedaboutit ?


參考解法

方法 1:

You should include the charset that the page is encoded in. You'll want to be sure that you're telling the truth. For instance, there are a lot pages running around without a charset designation (and therefore being treated as UTF-8 or ISO-8859-1) which are actually encoded as Windows-1252. That's fine as long as you stick to character codes they have in common (certainly 32-127 and all the important control characters like newline, tab, etc.). But you start with any accented letters or special symbols, and suddenly your page doesn't look right cross-browser.

This article on charsets and Unicode by Joel Spolsky is well worth a read, if you haven't already.

方法 2:

Setting encoding in HTTP headers does not encode the page. It only tells browsers how the page is encoded and how they should treat it. So set the encoding in which the page is encoded.

If you want to decide which encoding to use, I would recommend UTF-8. 

You can display all alphabetic characters of all languages (and much more) in UTF-8 encoding. There isn't any reason for use different encoding unless your pages need to be displayed by a device which does not support UTF-8 (such a device probably does not exist) or you have some very special requirements.

The performance impact of using different encoding is negligible as well as the page size.

(by PacerierT.J. CrowderPetr Peller)

參考文件

  1. Deciding charset for HTTP Headers. Should i simply put utf-8 and fuggedaboutit? (CC BY-SA 3.0/4.0)

#character-encoding #javascript #performance #html






相關問題

android webview顯示windows-1250字符集html的問題 (Trouble with android webview displaying windows-1250 charset html)

SQL Server 2008:字符編碼 (SQL Server 2008 : Character encoding)

刪除不可打印的字符 (Removing non-printable character)

電子郵件客戶端如何讀取內容類型標頭進行編碼? (How does an email client read the content-type headers for encoding?)

帶有 iText 7 的 PDF 中的希臘字符 (Greek characters in PDF with iText 7)

如何在 C 字符串中的文本或字母中添加下標字符? (How to add a subscript character to text or a letter in a C string?)

來自 URL 編碼問題的 NSArray (NSArray from URL encoding problem)

網絡上有免費提供的 HTML URL 編碼功能嗎?(在 C 中實現) (Is there any HTML URL encoding function freely available on web?? (Implementation in C))

讀取未知編碼的文本行 (Reading lines of text in unknown encoding)

Python - 以 Python 可以使用的格式編碼外來字符的方法? (Python - Way to encode foreign characters in format Python can work with?)

決定 HTTP 標頭的字符集。我應該簡單地把 utf-8 和 fuggedaboutit 放在一起嗎? (Deciding charset for HTTP Headers. Should i simply put utf-8 and fuggedaboutit?)

如何在 python 中將原始 unicode 轉換為 utf8-unicode? (How to convert raw unicode to utf8-unicode in python?)







留言討論