使用 Javascript 以字符串形式获取 HTML 的 DocType

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6088972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:24:52  来源:igfitidea点击:

Get DocType of an HTML as string with Javascript

javascriptdom

提问by matte

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

我知道我可以通过document.doctypeor访问 doctype 对象,document.childNodes[0]但我的问题是将 doctype 作为字符串获取。我可以在 chrome 和 safari 中通过调用document.doctypewhich 返回来做到这一点<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">。但是在 Firefox 中,调用document.doctype返回 DocumentType 对象。

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

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

Thanks!

谢谢!

回答by Rob W

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

在所有兼容的浏览器(包括 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 + '"' : '')
         + '>';

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

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

  • <!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">
  • <!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

回答by Scott

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

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

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

回答by codeassembly

function get_doctype()
{
    var doctype = 
    '<!DOCTYPE ' + 
    document.doctype.name +
    (document.doctype.publicId?' PUBLIC "' +  document.doctype.publicId + '"':'') +
    (document.doctype.systemId?' "' + document.doctype.systemId + '"':'') + '>';
    return doctype;
}

回答by T1000

Is that what are you looking for ?

这就是你要找的吗?

alert(document.doctype.publicId);

回答by KooiInc

Concatenate DocumentType.name, .publicIdand .systemId. Something like:

连接DocumentType.name,.publicId.systemId。就像是:

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