如果在内容类型标头中发送字符集,仍然建议通过元标记在 head 标记中发送?
我不再有该链接,因为它是几个月前的,但是有一篇关于设置发送到用户代理的正文的字符集的精彩而详细的文章。
本文的要点是,如果可能,请通过内容类型标头发送它,因为用户代理解析器在开始解析正文之前会知道它是(例如)utf-8 等。
通过元标记发送它意味着它必须等待找到所述元标记,然后返回到文档顶部并再次开始重新解析文档。
因此,很长一段时间以来,我们一直通过内容类型标头发送字符集,就像这样(无论如何在 PHP 中):
header('Content-type: text/html; charset=utf-8');
一切都很好。据我们所知,所有常用的用户代理中的一切都应该如此。
我的问题是,作为备份/故障安全/以防万一,我们仍然应该发送字符集元标记吗?使用元标记再次发送它是否会破坏通过内容类型标头发送它的目的?
编辑:这不是确切的文章,但重申了这一点(http://code.google.com/speed/page-speed/docs/rendering.html#SpecifyCharsetEarly ),如果可能,通过标头发送字符集。
I do not have the link anymore as it was a few months back, but there was a great and detailed article about setting the charset of the body being sent to the user-agent.
The gist of the article was that when possible, send it via the content-type header since the user-agents parser will know before the body begins being parsed that it is (for example) utf-8, etc.
Sending it via meta tag means it has to wait for said meta tag to be found, then return to the top of document and begin reparsing the document again.
So for quite a while now, we have been sending the charset via content-type header, like so (in PHP anyways):
header('Content-type: text/html; charset=utf-8');
All works great. As far as we can see everything is as should be in all the commonly used user-agents.
My question is this, as a backup / fail-safe / just in case, should we still send the charset meta tag? Will sending it again using the meta tag defeat the purpose of sending it via the content-type header?
edit: This is not the exact article, but re-iterates the point ( http://code.google.com/speed/page-speed/docs/rendering.html#SpecifyCharsetEarly ) that when possible, send charset via header.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我总是尝试这样做,因为它使文档更便携。例如,从本地文件查看时,它的显示方式与通过 Web 服务器传送时的显示方式相同。
Yes, I always try to do this since it makes the document more portable. E.g. It will display the same way when viewing from a local file, as it does when delivered via a web server.
是的,在标记中提供指定编码的元标记始终是一个好习惯。它应该是
之后的第一个标签。
Yes, it is always good practice to provide a meta tag in your markup which specifies the encoding. It should be the first tag after the
<head>
.