如何使用javascript或jquery从iframe获取完整的源代码? [英] How to get full source code from an iframe with javascript or jquery?
问题描述
我很无助.我需要使用Javascript或Jquery从iframe获取源代码.
I am helpless. I need to get the source code from an iframe with Javascript or Jquery.
iframe的源文件位于同一域中.这是我的iframe:
The source file of the iframe is in the same domain. Here is my iframe:
<iframe src="/source_file.html" id="iframe_id"></iframe>
我正在使用以下著名语法从iframe获取源代码:
I am using the following so famous syntax to get the source code from the iframe:
document.getElementById("iframe_id").contentWindow.document.body.innerHTML
但是使用此代码,我只能在BODY元素中获取代码.我想从<!doctype html>获取完整的代码.到< html> .
But with this code, I am only able to get the code inside BODY element. I want to get full code from <!doctype html> to <html>.
请帮助我.
推荐答案
使用 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
.
这篇关于如何使用javascript或jquery从iframe获取完整的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!