Skip to content

Instantly share code, notes, and snippets.

@AndorChen
Created December 2, 2011 04:26
Show Gist options
  • Save AndorChen/1421755 to your computer and use it in GitHub Desktop.
Save AndorChen/1421755 to your computer and use it in GitHub Desktop.

Revisions

  1. AndorChen created this gist Dec 2, 2011.
    49 changes: 49 additions & 0 deletions README.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    #link 没有 charset 属性

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

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

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

    > This attribute (`charset`) specifies the character encoding of the resource
    > designated by the link.
    这么看来这个属性的存在是完全符合标准的,不过在 [SitePoint 的 HTML Reference][2]
    中可以看到,目前的主流浏览器,包括早前的版本,都没有实现对该属性的支持。

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

    > 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][4] 是属性集,不包括 `charset` 属性。所以,在 HTML5 中,
    `link` 是没有 `charset` 这一属性的。

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

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

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


    [1]:http://www.w3.org/TR/html401/struct/links.html#adef-charset
    [2]:http://reference.sitepoint.com/html/link/charset
    [3]:http://www.w3schools.com/tags/att_link_charset.asp
    [4]:http://developers.whatwg.org/elements.html#global-attributes