Skip to content

Instantly share code, notes, and snippets.

@AndorChen
Created December 2, 2011 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndorChen/1421755 to your computer and use it in GitHub Desktop.
Save AndorChen/1421755 to your computer and use it in GitHub Desktop.
No charset in link

#link 没有 charset 属性

相信不少人都见过下面的 link 代码片段:

<link rel="stylesheet" href="stylesheet.css" type="text/css" charset="utf-8" />

其中比较陌生的是 charset 属性。在 HTML 4.01 的规范文档中有这样的记述:

This attribute (charset) specifies the character encoding of the resource
designated by the link.

这么看来这个属性的存在是完全符合标准的,不过在 SitePoint 的 HTML Reference 中可以看到,目前的主流浏览器,包括早前的版本,都没有实现对该属性的支持。

W3Schools 相应的页面这证实了这一点。

The charset attribute is not supported by any of the major browsers.

上面是针对 HTML4.01,再来看看目前比较流行的 HTML5,在 HTML5 的规范文档中定义了 link 的合法属性:

Content attributes:
Global attributes
href
rel
media
hreflang
type
sizes

其中 Global attributes 是属性集,不包括 charset 属性。所以,在 HTML5 中, link 是没有 charset 这一属性的。

HTML5 制定原则之一是:“Priority of constituencies”。意思是,首先考虑最终受众和网 页制作人员,而非标准的制定者和实现者(浏览器制造商)。“浏览器制造商是判决者,如 果他们拒绝实现某一提案,那就没必要将这个提案加入标准。”

既然没有浏览器愿意支持 linkcharset 属性,那么就没必要将其写入 HTML5 的规 范。

所以,在 HTML 4.01 或 HTML5 中,link 都是没必要写 charset 属性的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment