html - script 标签的 charset 属性的默认值是多少?

标签 html character-encoding

假设我有这样一个脚本: <script type="text/javascript" src="myScript.js">

我在网上看到一些消息来源声称,如果 charset属性被省略,它默认为 ISO-8859-1。我见过其他人声称它采用与包含脚本标记的 HTML 页面相同的编码。真相是什么?

我需要知道,因为我的 JavaScript 文件包含将插入到 HTML 中的文字字符串,其中包括非 ASCII 字符,例如欧元符号 (€)。我意识到添加字符集属性或仅对这些字符进行 HTML 编码应该可以解决我的问题,但我仍然想了解默认行为。

编辑:为了澄清一点,我不仅需要知道标准所说的内容,还需要知道浏览器的实际行为。此处描述的行为:http://joconner.com/2008/09/javascript-file-encoding/似乎暗示浏览器总是采用 ISO-8859-1。

最佳答案

w3c 有一个浏览器确定字符编码的标准方法,您可以在这里阅读:http://www.w3.org/TR/html4/charset.html#spec-char-encoding

To sum up, conforming user agents must observe the following priorities when determining a document's character encoding (from highest priority to lowest):

  1. An HTTP "charset" parameter in a "Content-Type" field.
  2. A META declaration with "http-equiv" set to "Content-Type" and a value set for "charset".
  3. The charset attribute set on an element that designates an external resource.

In addition to this list of priorities, the user agent may use heuristics and user settings. For example, many user agents use a heuristic to distinguish the various encodings used for Japanese text. Also, user agents typically have a user-definable, local default character encoding which they apply in the absence of other indicators.

关于html - script 标签的 charset 属性的默认值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5004865/

相关文章:

html - CSS 不覆盖用户代理按钮样式

html - 列表元素item调整为ul高度

javascript - 通过特定坐标将 DIV/Span 标签应用于单词

html - 网页在 chrome 中正确显示,而不是在 firefox 中

javascript - 完全删除 jquery ajax 调用上的所有类型的编码/默认/自动编码

javascript - PHP 中的 JS charCodeAt 等价物(具有完整的 unicode 和 emoji 兼容性)

c++ - Libtiff:如何获取像素值或如何将 TIFF 文件转换为文本文件

javascript - 单击特定元素时暂停 HTML 音频

python - Kivy(python-for-android)缺少编解码器

java - Perl/Java 字节编码差异