如何使用 devtools 或 JavaScript 代码段判断加载的页面是否为 XML 处理的 XHTML

How to tell if loaded page is true XML-processed XHTML or not, using devtools or JavaScript snippet

提问人:myf 提问时间:9/15/2023 更新时间:9/15/2023 访问量:47

问:

检查连接到远程服务器失败时显示的 Firefox 系统页面显示明显的迹象表明它是真实的页面:application/xhtml+xml

Developer Tools - Problem loading page - http://localhost:4200 inspecting DOM tree. All elements in expanded head (meta, link) have closing tags, what would not show in regular HTML

有显式的,所有元素,甚至是 void 元素,都有结束标记,所以它很可能被处理为 XML。这就带来了一个问题:xmlns="http://www.w3.org/1999/xhtml"

对于在此类页面中加载的脚本或开发人员工具命令,将XML处理的页面与常规HTML区分开来的最简单方法是什么?

在这两种情况下,检查属性都会给出。document.documentElement.namespaceURI"http://www.w3.org/1999/xhtml"

  • 什么 DOM 属性或方法可以用作签名页是 XML?
  • 或者是否需要一些更复杂的检测过程(例如做一些专门在XML中工作的DOM/XSLT/实体的东西)才能确定?

所描述的系统页面的来源可能是,但是,当直接显示时,它不起作用。view-source:chrome://global/content/aboutNetError.xhtml

javascript html xml、 xhtml 检测

评论

0赞 Martin Honnen 9/15/2023
显示什么? 或?document.contentTypeapplication/xhtml+xmltext/html
0赞 Siebe Jongebloed 9/15/2023
您可以使用本页中描述的方法:geeksforgeeks.org/...,并检查 /html/head/link 元素。在 xhtml 中,它们应该是自闭合的,但在 html 中则不是
0赞 myf 9/15/2023
@MartinHonnen确实显示了感兴趣的模仿类型(对于相关页面)。很好,这就是我想要的答案,谢谢。(你介意把它作为真正的答案重复一遍,这样我就可以接受它了吗?document.contentType"application/xhtml+xml"

答:

1赞 Martin Honnen 9/15/2023 #1

document.contentType 应该提供 XML 解析的 XHTML 文档和 HTML(5) 解析的 HTML 文档。"application/xhtml+xml""text/html"

规格:WHATWG:contentType,文档:MDN:文档:contentType 属性

演示:

This document's <code>contentType</code>:<br/>
<script>document.body.append(document.contentType)</script>

<hr>

<p>Iframe with minimal XHTML and same content:</p>

<iframe src='data:application/xhtml+xml,
  <html xmlns="http://www.w3.org/1999/xhtml"><body>
  This document%27s <code>contentType</code>:<br/>
  <script>document.body.append(document.contentType) </script>
  </body></html>
'></iframe>