是否有专业人士使用严格的 HTML 4.01 而不是严格的 XHTML 1.0 (content="text/html)?

发布于 2024-08-18 22:51:24 字数 68 浏览 3 评论 0 原文

是否有专业人士使用严格的 HTML 4.01 而不是严格的 XHTML 1.0 (content="text/html)?

Are there any pros to use HTML 4.01 strict over XHTML 1.0 strict (content="text/html)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佼人 2024-08-25 22:51:24

当然。您的代码会更小,因为您不需要在单字属性上包含额外的 " 字符,在自闭合元素上包含 / 字符,您可以省略各种开始和结束标记,例如

,甚至 等为 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 规范的当前编辑者(其中一些原因仍然有效,其中一些不再相关)。

以下是一些示例:

  1. 格式良好的 XHTML,用作 application/xhtml+xml< /code>
  2. 格式良好的 XHTML,用作 文本/html
  3. 格式不正确的 XHTML,用作 application/xhtml+xml
  4. 不太好- 形成的 XHTML,用作 text/html
  5. 嗯-形成 HTML5,作为 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 标记,例如

这并不是 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 as text/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:

  1. Well-formed XHTML, served as application/xhtml+xml
  2. Well-formed XHTML, served as text/html
  3. Not well-formed XHTML, served as application/xhtml+xml
  4. Not well-formed XHTML, served as text/html
  5. Well-formed HTML5, served as 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-closing script 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):

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.

放低过去 2024-08-25 22:51:24

是的:当您以 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文