获得"未声明的字符编码" Firefox中的错误

时间:2014-05-17 21:32:31

标签: javascript html firefox character-encoding

我正在使用JS SPA book并注意到我在chrome vs firefox中获得了不同的结果。这是代码......

<!doctype html>
<html>
<head>
  <title>SPA Chapter 1 section 1.2.2</title>
  <style type="text/css"></style>
  <script type="text/javascript">
    var prison = function () {
      console.log('made it to prison');
    };
    prison();
  </script>
</head>
<body>
  <div id="spa"></div>
</body>
</html>

美好而简单......不是吗?在chrome中我得到了预期的结果&#34;使它进入监狱&#34;在控制台中。但是,在FF中只会出现读取错误...

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

是什么给出的?我是否在FF webdev控制台中缺少一个设置?

1 个答案:

答案 0 :(得分:1)

当您在开发工具中启用控制台日志记录时,Firefox会在控制台日志中显示预期结果。它还显示一条消息,在某些版本中归类为错误消息但不影响功能。它实际上是一条信息性消息,表示页面的字符编码没有以任何方式声明;请参阅W3C page on encodings

控制控制台日志中显示内容的方法取决于您使用的开发工具。使用当前版本的Firefox中的built-in dev tools,称为Aurora,可以使用 Ctrl Shift K 调用,确保启用“Logging”(以黑色显示,而不是灰色)在这张图片中:

enter image description here

您需要决定字符编码。对于任何现代应用,很少需要考虑除UTF-8之外的任何其他选项。您可以通过在文本编辑器中将HTML文件保存为“带BOM的UTF-8”和/或使用<meta charset=utf-8>部分中的标记head来声明它。如果您只使用后者,并且如果页面是通过HTTP发送的,则应确保服务器不会在Content-Type HTTP标头中发送冲突信息。