ここですでに説明していることは知っていますが、ドキュメント全体(doctypeを含む)を取得するための解決策はありませんでした。
$(document).html();
戻りますnull
..。
ここですでに説明していることは知っていますが、ドキュメント全体(doctypeを含む)を取得するための解決策はありませんでした。
$(document).html();
戻りますnull
..。
これにより、すべてのHTMLが取得されます。
document.documentElement.outerHTML
残念ながら、Doctypeは返されません。しかし、あなたはdocument.doctype
それを手に入れて、2つを一緒に接着するために使うことができます。
できるよ
new XMLSerializer().serializeToString(document);
IE9より新しいすべてのブラウザー用
これを試して。
$("html").html()
documentは変数であり、htmlタグを表していません。
編集
Doctypeを取得するには、次のように使用できます。
document.doctype
これはIE6+でサポートされている関数であり、それouterHTML
以上のサポートには使用されません。Doctypeが追加され、いくつかのトリックを使用してhtml
タグとその属性を取得します。doctypeの文字列を受け取るために、使用しないouterHTML
ので、すべてのブラウザをサポートします。html
タグを取得するためにいくつかのトリックを使用します。このコードを追加します:
document.fullHTML = function () {
var r = document.documentElement.innerHTML, t = document.documentElement.attributes, i = 0, l = '',
d = '<!DOCTYPE ' + document.doctype.name + (document.doctype.publicId ? ' PUBLIC "' + document.doctype.publicId + '"' : '') + (!document.doctype.publicId && document.doctype.systemId ? ' SYSTEM' : '') + (document.doctype.systemId ? ' "' + document.doctype.systemId + '"' : '') + '>';
for (; i < t.length; i += 1) l += ' ' + t[i].name + '="' + t[i].value + '"';
return d+'\n<html' + l + '>' + r + '</html>';
}
これで、次の関数を実行できます。
console.log(document.fullHTML());
これにより、HTMLとDoctypeが返されます。
これをexample.comで実行しました。結果は次のとおりです。
完全なドキュメントを取得するかどうかはわかりませんが、できることは、htmlタグのコンテンツとdoctypeを別々に取得できることです。
$('html').html() for content
doctypeを取得するためのdocument.doctype
ドキュメント全体(Doctypeを含む)に直接アクセスできるとは思いませんが、これは機能します:
$.get(document.location, function(html) {
// use html (which is the complete source code, including the doctype)
});
私はブラウザのコンソールでそれをしました
document.documentElement;