且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用javascript或jquery从iframe获取完整的源代码?

更新时间:2023-10-31 11:25:04

使用 outerHTML 而不是innerHTML.

示例:

document.getElementById("iframe_id").contentWindow.document.documentElement.outerHTML

请注意,这将排除doctype和不是HTML节点的子代的任何注释.如果您需要包含此数据,则需要进行单独的AJAX请求以获取数据.还可以从 document.doctype 重构文档类型.

Note that this will exclude the doctype and any comments that are not a child of the HTML node. If you need to include this data, you will need to make a separate AJAX request to fetch it. The doctype could also be reconstructed from document.doctype.