使用 Javascript 将 HTML 的 DocType 作为字符串获取

发布于 2024-11-09 08:28:10 字数 336 浏览 0 评论 0原文

我知道我可以通过 document.doctypedocument.childNodes[0] 访问 doctype 对象,但我的问题是将 doctype 作为字符串获取。我可以在 chrome 和 safari 中通过调用 document.doctype 来执行此操作,它返回 。然而在 Firefox 中,调用 document.doctype 返回 DocumentType 对象。

有没有办法在所有浏览器中获取 doctype 字符串,如 chrome 和 safari 中?

谢谢!

I know that I can access to doctype object via document.doctype or document.childNodes[0] but my problem is getting doctype as a string. I can do this in chrome and safari by calling document.doctype which returns <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">. However in Firefox, calling document.doctype returns DocumentType object.

Is there a way to get the doctype string in all browsers as in chrome and safari?

Thanks!

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

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

发布评论

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

评论(5

八巷 2024-11-16 08:28:10

在所有兼容的浏览器(包括 Chrome/Safari)中,document.doctype 还返回 DocumentType 对象。以下代码可用于生成有效的 DOCTYPE 字符串。

var node = document.doctype;
var html = "<!DOCTYPE "
         + node.name
         + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
         + (!node.publicId && node.systemId ? ' SYSTEM' : '') 
         + (node.systemId ? ' "' + node.systemId + '"' : '')
         + '>';

此方法返回 有效 (HTML5) doctypes 的正确字符串,例如:

  • < ;!DOCTYPE html>

代码说明:

node.name      # Holds the name of the root element, eg: HTML / html
node.publicId  # If this property is present, then it's a public document type.
               #>Prefix PUBLIC
!node.publicId && node.systemId
               # If there's no publicId, but a systemId, prefix SYSTEM
node.systemId  # Append this if present

In all compliant browsers (including Chrome/Safari), document.doctype also returns a DocumentType object. The following code can be used to generate a valid DOCTYPE string.

var node = document.doctype;
var html = "<!DOCTYPE "
         + node.name
         + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
         + (!node.publicId && node.systemId ? ' SYSTEM' : '') 
         + (node.systemId ? ' "' + node.systemId + '"' : '')
         + '>';

This method returns the correct string for valid (HTML5) doctypes, eg:

  • <!DOCTYPE html>
  • <!DOCTYPE html SYSTEM "about:legacy-compat">
  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">

Explanation of the code:

node.name      # Holds the name of the root element, eg: HTML / html
node.publicId  # If this property is present, then it's a public document type.
               #>Prefix PUBLIC
!node.publicId && node.systemId
               # If there's no publicId, but a systemId, prefix SYSTEM
node.systemId  # Append this if present
丑疤怪 2024-11-16 08:28:10

您还可以使用这一行来获取当前的文档类型。这适用于任何现代浏览器以及 IE 9 及更高版本

new XMLSerializer().serializeToString(document.doctype);

You can also use this one liner to get the current doctype. This will work in any modern browser and IE 9 and higher.

new XMLSerializer().serializeToString(document.doctype);
离线来电— 2024-11-16 08:28:10
function get_doctype()
{
    var doctype = 
    '<!DOCTYPE ' + 
    document.doctype.name +
    (document.doctype.publicId?' PUBLIC "' +  document.doctype.publicId + '"':'') +
    (document.doctype.systemId?' "' + document.doctype.systemId + '"':'') + '>';
    return doctype;
}
function get_doctype()
{
    var doctype = 
    '<!DOCTYPE ' + 
    document.doctype.name +
    (document.doctype.publicId?' PUBLIC "' +  document.doctype.publicId + '"':'') +
    (document.doctype.systemId?' "' + document.doctype.systemId + '"':'') + '>';
    return doctype;
}
双马尾 2024-11-16 08:28:10

这就是您要找的吗?

alert(document.doctype.publicId);

Is that what are you looking for ?

alert(document.doctype.publicId);
留一抹残留的笑 2024-11-16 08:28:10

连接 DocumentType.name.publicId.systemId。像这样的东西:

'<!DOCTYPE '+ 
  DocumentType.name+' PUBLIC "'+ //maybe you should check for publicId first
  DocumentType.publicId+'" "'+
  DocumentType.systemId+'">'

Concatenate DocumentType.name, .publicId and .systemId. Something like:

'<!DOCTYPE '+ 
  DocumentType.name+' PUBLIC "'+ //maybe you should check for publicId first
  DocumentType.publicId+'" "'+
  DocumentType.systemId+'">'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文