Html 如何修复“在 UTF-8 文件中找到的字节顺序标记”验证警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2554445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix "Byte-Order Mark found in UTF-8 File" validation warning
提问by rsturim
I've got an xhtml page validating under xhtml strict doctype -- but, I getting this warning which I trying to understand -- and correct.
我有一个在 xhtml strict doctype 下验证的 xhtml 页面——但是,我收到了这个我试图理解的警告——并且更正了。
Just, how do I locate this errant "Byte-Order Mark". I'm editing my file using Visual Studio--not sure if that helps.
只是,我如何找到这个错误的“字节顺序标记”。我正在使用 Visual Studio 编辑我的文件——不确定这是否有帮助。
Warning Byte-Order Mark found in UTF-8 File.
The Unicode Byte-Order Mark (BOM) in UTF-8 encoded files is known to cause problems for some text editors and older browsers. You may want to consider avoiding its use until it is better supported.
在 UTF-8 文件中发现警告字节顺序标记。
已知 UTF-8 编码文件中的 Unicode 字节顺序标记 (BOM) 会导致某些文本编辑器和旧浏览器出现问题。您可能需要考虑避免使用它,直到它得到更好的支持。
回答by T.J. Crowder
The location part of your question is easy: The byte-order mark(BOM) will be at the very beginning of the file.
您问题的位置部分很简单:字节顺序标记(BOM) 将位于文件的最开头。
When editing the file, go to File | Advanced Save Options...
and you should find an "Encoding" drop-down (along with a "Line endings" drop-down). It's probably set to use "Unicode (UTF-8 with signature) - Codepage 65001". If you scroll down a fair bit, you can find "Unicode (UTF-8 without signature) - Codepage 65001". That should do it (if you want to). Some systems may be confused by a BOM on a UTF-8 file, as the warning indicates.
编辑文件时,转到File | Advanced Save Options...
“编码”下拉菜单(以及“行尾”下拉菜单)。它可能设置为使用“Unicode(带签名的 UTF-8) - 代码页 65001”。如果向下滚动一点,您可以找到“Unicode(无签名的 UTF-8)- 代码页 65001”。应该这样做(如果您愿意)。如警告所示,某些系统可能会被 UTF-8 文件上的 BOM 混淆。
See also this notein the Unicode site's FAQ about the BOM and UTF-8 files. It has no function other than to call out that the file is, in fact, UTF-8. In particular, it has no effect on the byte order (the main reason we have BOMs), because the byte order of UTF-8 is fixed.
另请参阅Unicode 站点关于 BOM 和 UTF-8 文件的常见问题解答中的此注释。除了指出该文件实际上是 UTF-8 之外,它没有任何功能。特别是它对字节顺序没有影响(我们有 BOM 的主要原因),因为 UTF-8 的字节顺序是固定的。
回答by Taller Digital VW
回答by Leo
For someone using Visual Studio seeing the annoying red dot on bitbucket on 2018, just go to Visual Studio to "File" -> "File.cshtml Save As..." and select "Save with Encoding...":
对于使用 Visual Studio 的人在 2018 年在 bitbucket 上看到烦人的红点,只需转到 Visual Studio 到“文件”->“File.cshtml 另存为...”并选择“使用编码保存...”:
Then it will popup a screen so that you can change the Encoding, try and look all the way down in the list until you see "Unicode (UTF-8 without signature) - Codepage 65001":
然后它会弹出一个屏幕,以便您可以更改编码,尝试在列表中一直向下查看,直到看到“Unicode(无签名的 UTF-8) - 代码页 65001”:
After this, just overwrite your file and upload it to your repo and the BOM will be gone.
在此之后,只需覆盖您的文件并将其上传到您的仓库,BOM 就会消失。
Hope it helps. Leo.
希望能帮助到你。狮子座。
回答by japetko
In Linux:
在Linux 中:
Open the file with Geany.
用Geany打开文件。
In the menu "Dokument" uncheck "Write Unicode BOM".
在菜单“ Dokument”中取消选中“ Write Unicode BOM”。
Savethe file.
保存文件。
回答by darekk
BOM sometimes is located INSIDE text, not at the beginning - if a file has been assembled some time by php from other files using for example include_once(). To remove it, delete area between at least one character before BOM and at least one character after BOM (just in case). Position of BOM can be located in F12 Developer Tools of the Internet Explorer and probably Edge. It is visualised as a black diamond / rhombus.
BOM 有时位于 INSIDE 文本中,而不是位于开头 - 如果某个文件已由 php 从其他文件中使用例如include_once()组装一段时间。要删除它,请删除 BOM 前至少一个字符和 BOM 后至少一个字符之间的区域(以防万一)。BOM 的位置可以在 Internet Explorer 的 F12 Developer Tools 中,也可能在 Edge 中。它被形象化为一个黑色的菱形/菱形。
Visual Studio and WebMatrix can save files with or without signature (at the beginning).
Visual Studio 和 WebMatrix 可以保存带有或不带有签名的文件(在开始时)。
BOM causes errors during validation ( https://validator.w3.org/#validate_by_upload) or in consoles - </HEAD> can be treated as orphaned element without <HEAD>, when apparently is present !:
BOM 在验证期间 ( https://validator.w3.org/#validate_by_upload) 或在控制台中导致错误- </HEAD> 可以被视为没有 <HEAD> 的孤立元素,当显然存在时!:
Error: Stray end tag head.
错误:杂散的结束标记头。
<BODY> as second one <BODY>, when only one <BODY> exists and everything is correct:
<BODY> 作为第二个 <BODY>,当只有一个 <BODY> 存在并且一切正常时:
Error: Start tag body seen but an element of the same type was already open.
错误:已看到起始标记正文,但已打开相同类型的元素。
And entire document can be seen lacking DOCTYPE, when BOM or two BOMS occupy first line and DOCTYPE is in second line, with a message similar to this one:
并且可以看到整个文档缺少DOCTYPE,当BOM或两个BOMS占据第一行而DOCTYPE在第二行时,会出现类似这样的信息:
Error: Non-space characters found without seeing a doctype first. Expected e.g. <!DOCTYPE html>.
Error: Element head is missing a required instance of child element title.
Error: Stray doctype.
Error: Stray start tag html.
Error: Stray start tag head.
Error: Attribute name not allowed on element meta at this point.
Error: Element meta is missing one or more of the following attributes: itemprop, property.
Error: Attribute http-equiv not allowed on element meta at this point.
Error: Element meta is missing one or more of the following attributes: itemprop, property.
Error: Attribute name not allowed on element meta at this point.
Error: Element meta is missing one or more of the following attributes: itemprop, property.
Error: Element link is missing required attribute property.
Error: Attribute name not allowed on element meta at this point.
Error: Element meta is missing one or more of the following attributes: itemprop, property.
Error: Attribute name not allowed on element meta at this point.
Error: Element meta is missing one or more of the following attributes: itemprop, property.
Error: Attribute name not allowed on element meta at this point.
Error: Element meta is missing one or more of the following attributes: itemprop, property.
Error: Element title not allowed as child of element body in this context. (Suppressing further errors from this subtree.)
Error: Element style not allowed as child of element body in this context. (Suppressing further errors from this subtree.)
Error: Stray end tag head.
Error: Start tag body seen but an element of the same type was already open.
Fatal Error: Cannot recover after last error. Any further errors will be ignored.
错误:在没有先看到文档类型的情况下发现了非空格字符。应为例如 <!DOCTYPE html>。
错误:元素头缺少子元素标题的必需实例。
错误:杂散的文档类型。
错误:杂散的开始标记 html。
错误:杂散开始标记头。
错误:此时元素元不允许属性名称。
错误:元素元缺少以下一项或多项属性:itemprop、property。
错误:此时元素元不允许使用属性 http-equiv。
错误:元素元缺少以下一项或多项属性:itemprop、property。
错误:此时元素元不允许属性名称。
错误:元素元缺少以下一项或多项属性:itemprop、property。
错误:元素链接缺少必需的属性。
错误:此时元素元不允许属性名称。
错误:元素元缺少以下一项或多项属性:itemprop、property。
错误:此时元素元不允许属性名称。
错误:元素元缺少以下一项或多项属性:itemprop、property。
错误:此时元素元不允许属性名称。
错误:元素元缺少以下一项或多项属性:itemprop、property。
错误:在此上下文中不允许元素标题作为元素主体的子元素。(抑制来自此子树的更多错误。)
错误:在此上下文中不允许元素样式作为元素主体的子元素。(抑制来自此子树的更多错误。)
错误:杂散的结束标记头。
错误:已看到起始标记正文,但已打开相同类型的元素。
致命错误:上次错误后无法恢复。任何进一步的错误都将被忽略。
( https://validator.w3.org/#validate_by_uri)
( https://validator.w3.org/#validate_by_uri)
And stream of messages in IE F12 Developer Tools console:
以及 IE F12 开发人员工具控制台中的消息流:
HTML1527: DOCTYPE expected. Consider adding a valid HTML5 doctype: "<!DOCTYPE html>".
HTML1502: Unexpected DOCTYPE. Only one DOCTYPE is allowed and it must occur before any elements.
HTML1513: Extra "<html>" tag found. Only one "<html>" tag should exist per document.
HTML1503: Unexpected start tag. HTML1512: Unmatched end tag.
HTML1527:应为 DOCTYPE。考虑添加一个有效的 HTML5 文档类型:“<!DOCTYPE html>”。
HTML1502:意外的 DOCTYPE。只允许一个 DOCTYPE 并且它必须出现在任何元素之前。
HTML1513:发现额外的“<html>”标签。每个文档应该只存在一个“<html>”标签。
HTML1503:意外的开始标记。HTML1512:不匹配的结束标记。
Everything caused by one BOM at the beginning. And Debugger shows one black rhombus in the first line.
一切都是由一开始的一个 BOM 造成的。Debugger 在第一行显示一个黑色菱形。
Files saved with signature, but not assembled by php don't cause such errors and black diamonds are not vissible in IE debugger. So perhaps php transforms BOM somehow. It seems that main php file must be saved with signature to see this.
使用签名保存但未由 php 组装的文件不会导致此类错误,并且黑色菱形在 IE 调试器中不可见。所以也许php以某种方式转换了BOM。似乎必须使用签名保存主 php 文件才能看到这一点。
Those strange characters occur at the beginning and/or on the borders of files merged with include_once() and are not visible when files are saved before without signature. This is why it points at BOM involvement.
这些奇怪的字符出现在与 include_once() 合并的文件的开头和/或边界上,并且在之前没有签名的情况下保存文件时不可见。这就是它指出 BOM 参与的原因。
I have noticed this everything day before yesterday when started converting my website to HTML5 and validating.
前天开始将我的网站转换为 HTML5 并进行验证时,我已经注意到了这一点。
BOM can also create a small indent at the beginning of line. Two files containing identical text but one with indent.
BOM 还可以在行首创建一个小缩进。两个包含相同文本但一个缩进的文件。