是否有专业人士使用严格的 HTML 4.01 而不是严格的 XHTML 1.0 (content="text/html)?
是否有专业人士使用严格的 HTML 4.01 而不是严格的 XHTML 1.0 (content="text/html)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否有专业人士使用严格的 HTML 4.01 而不是严格的 XHTML 1.0 (content="text/html)?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
当然。您的代码会更小,因为您不需要在单字属性上包含额外的
"
字符,在自闭合元素上包含/
字符,您可以省略各种开始和结束标记,例如、
,甚至
、
、
等为 HTML 4.01可选 >(以及HTML5)。 "http://perfectionkills.com/optimizing-html/" rel="nofollow noreferrer">优化 HTML 和 优化 优化 HTML 了解有关精简 HTML 的一些技巧;其中许多建议仅适用于 HTML 4.01 或 HTML5。
现在,并不是每个人都需要缩小这么多;但如果您这样做,使用 HTML 4.01(或 HTML5)而不是 XHTML 可能会有所帮助。
此外,Internet Explorer 实际上并不支持 XHTML。如果您将其发送为
application/xhtml+xml
,它只会尝试下载它。因此,如果您使用XHTML,您需要将其作为text/html
发送到IE,这将使浏览器将其解释为HTML。有关为什么 XHTML 是一个坏主意的有点过时的讨论,请参阅 Ian Hickson 的 XHTML 被认为有害, HTML5 规范的当前编辑者(其中一些原因仍然有效,其中一些不再相关)。以下是一些示例:
application/xhtml+xml< /code>
文本/html
application/xhtml+xml
text/html
text/html
注意,第一个在 IE 中不起作用。第二个适用于所有浏览器,但你只是浪费字节;例如,浏览器无法正确解释命名空间,因此,如果您将 XML 包含在另一个命名空间中,它实际上不会出现在脚本或 CSS 中的另一个命名空间中。第三个将显示一条大错误消息,第四个将显示得很好,尽管它不是格式良好的 XHTML(这表明浏览器正在使用其 HTML 解析器)。
通过使用 HTML 4.01 文档类型或 HTML5 文档类型,您可以获得与
text/html
示例完全相同的效果,如第五个示例中所示(如果您坚持 HTML,这也是有效的 HTML 4.01) 4.01 文档类型进入)。如果您使用 HTML 4.01 或 HTML5,则可以节省大量空间,并且您不会因为有时将文档处理为 XHTML、有时将其处理为 HTML 来欺骗自己。小心将 XHTML 作为
text/html
提供的另一个原因是,根据是由 HTML 解析器还是由 XML 解析器解析,它的处理方式有所不同。例如,自闭合script
标记,例如,在XHTML,如果由 XML 解析器解析,将被解析为空元素。然而,在 HTML 中,这将被简单地视为脚本开始标记,并且它将“吃掉”文档的其余部分,因为解析器假设它位于脚本内部,不断进行解析,直到找到结束标记。这可能不是您想要的,如果您有时将文档视为 XML,有时将其视为 HTML,则可能会造成麻烦。这是这个问题的一个例子;这两个文档在 Firefox 中显示不同,即使它们具有相同的内容(Safari 似乎对它们的处理方式相同,因此此问题的程度在不同浏览器之间有所不同):
标记充当
application/xhtml+xml
标记用作
text/html
这并不是 HTML 和 XHTML 解析器之间的唯一区别。在 HTML 中,
会将
隐式插入到 DOM 中,即使源代码中没有
;在 XHTML 中,如果您没有显式指定
,它不会出现在 DOM 中。
因此,将 XHTML 提供为
text/html
将使您的代码比仅使用 HTML 4.01 或 HTML5 时的代码更大,并且如果您有时将其处理为真正的 XML,有时将其处理,则可能会导致混乱它作为 HTML。Sure. Your code will be smaller, since you don't need to include the extra
"
characters on one-word attributes,/
characters on self-closing elements, you can leave out various opening and closing tags, such as</p>
,</li>
, and even<html>
,<head>
,<body>
, and so on are optional in HTML 4.01 (and HTML5). See Optimizing HTML and Optimizing Optimizing HTML for a few tips on stripping down your HTML; many of these pieces of advice work only in HTML 4.01 or HTML5.Now, not everyone needs to minify this much; but if you do, using HTML 4.01 (or HTML5) instead of XHTML can be beneficial.
Also, Internet Explorer doesn't actually support XHTML as XHTML; if you send it as
application/xhtml+xml
, it will just try to download it. So, if you use XHTML, you need to send it to IE astext/html
, which will make the browsers just interpret it as HTML. For a somewhat outdated discussion of why XHTML is a bad idea, see XHTML Considered Harmful by Ian Hickson, the current editor of the HTML5 specification (some of these reasons are still valid, some of them are no longer relevant).Here are a few examples:
application/xhtml+xml
text/html
application/xhtml+xml
text/html
text/html
Note that the first does not work in IE. The second works in all browsers, but you are just wasting bytes; the browsers don't interpret namespaces properly, for example, so if you include XML in another namespace, it won't actually appear in another namespace in your scripts or CSS. The third will display a big error message, and the fourth will display just fine even though it's not well-formed XHTML (which demonstrates that the browsers are using their HTML parsers).
You can get the exact same effect as the
text/html
examples by using an HTML 4.01 doctype, or an HTML5 doctype, as demonstrated in the fifth example (this is also valid HTML 4.01 if you stick the HTML 4.01 doctype into it). If you use HTML 4.01 or HTML5, you can save a lot of space, and you won't be fooling yourself by sometimes processing the document as XHTML and sometimes processing it HTML.Another reason to beware of serving XHTML as
text/html
is that it's processed differently depending on whether it's parsed by an HTML parser or an XML parser. For instance, a self-closingscript
tag, such as<script type="text/javascript" src="foo.js"/>
, is valid in XHTML, and if parsed by an XML parser, will be parsed as an empty element. In HTML, however, this will be seen simply as a script opening tag, and it will "eat" the rest of the document, as the parser keeps parsing assuming it's inside a script until it finds a close tag. This is likely not what you want, and can trip you up if you sometimes treat a document as XML and sometimes treat it as HTML. Here's an example of this problem; these two documents display differently in Firefox, even though they have the same contents (Safari appears to treat them the same, so this extent of this problem varies between different browsers):<script/>
tag in XHTML served asapplication/xhtml+xml
<script/>
tag in XHTML served astext/html
This is not the only difference between the HTML and XHTML parsers. In HTML, a
<table>
will have a<tbody>
inserted into the DOM implicitly even if you don't have one in the source; in XHTML, if you don't specify a<tbody>
explicitly, it will not be present in the DOM.So, serving XHTML as
text/html
will make your code larger than it would be if you just used HTML 4.01 or HTML5, and it can lead to confusion if you sometimes process it as real XML and sometimes treat it as HTML.是的:当您以 text/html 形式发送 HTML 4.01 时,您符合标准,但当您以 text/html 形式发送 XHTML 1.0 strict 时,则不符合标准。事实上,当您将 XHTML 作为 HTML 发送给浏览器时,浏览器会认为您正在向它们发送 HTML 4.01。
Yes: you're compliant to the standards when you send HTML 4.01 as text/html, but you're not when you send XHTML 1.0 strict as text/html. In fact, browsers believe you're sending them HTML 4.01 when you send them XHTML as HTML.